找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 3376|回復(fù): 5
收起左側(cè)

8051單片機(jī)內(nèi)部定時(shí)器/計(jì)數(shù)器程序問題

[復(fù)制鏈接]
ID:718035 發(fā)表于 2020-5-25 17:35 | 顯示全部樓層 |閱讀模式
實(shí)驗(yàn)設(shè)置8051單片機(jī)內(nèi)部定時(shí)器/計(jì)數(shù)器T0計(jì)數(shù),按方式2工作,對(duì)T0引腳(P3.4)手動(dòng)輸入的單脈沖進(jìn)行計(jì)數(shù)。并將其計(jì)數(shù)值從P1口輸出,在LED上顯示出來。驗(yàn)證其正確性。輸入我的代碼后,小燈泡不亮,不知道哪里錯(cuò)了。求各位大佬告知
代碼如下:#include<reg51.h>
#define uchar unsigned char
uchar code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
uchar num=0;
void t0isr() interrupt 1
{
num++;
num%=10;
P1=table[num];
}
main()
{
TMOD=0x06;
TH0=0xff;
TL0=0xff;
TR0=1;
ET0=1;
EA=1;
while(1);
}

回復(fù)

使用道具 舉報(bào)

ID:213173 發(fā)表于 2020-5-25 21:20 | 顯示全部樓層
程序沒有問題,只是初始化是少了一條P1=table[num];,但無關(guān)大局。
無標(biāo)題.jpg

  1. #include<reg51.h>
  2. #define uchar unsigned char
  3. uchar code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
  4. uchar num=0;

  5. void t0isr() interrupt 1
  6. {
  7.         num++;
  8.         num%=10;
  9.         P1=table[num];
  10. }
  11. void main()
  12. {
  13.         TMOD=0x06;
  14.         TH0=0xff;
  15.         TL0=0xff;
  16.         TR0=1;
  17.         ET0=1;
  18.         EA=1;
  19.         P1=table[num];
  20.         while(1);
  21. }
復(fù)制代碼



回復(fù)

使用道具 舉報(bào)

ID:94031 發(fā)表于 2020-5-25 21:22 | 顯示全部樓層
//計(jì)數(shù)0計(jì)數(shù),數(shù)碼管顯示,按鍵接P34
#include<reg51.h>
#define uchar unsigned char
uchar code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
uchar num=0;

main()
{
        TMOD|=0x05;
        TR0=1;
       
        while(1)
        {
                num=TL0;
                P1=table[num];
                if(num>9){TL0=0;}
        }
}
回復(fù)

使用道具 舉報(bào)

ID:718035 發(fā)表于 2020-5-26 13:41 | 顯示全部樓層
wulin 發(fā)表于 2020-5-25 21:20
程序沒有問題,只是初始化是少了一條P1=table[num];,但無關(guān)大局。

多謝您的幫忙
回復(fù)

使用道具 舉報(bào)

ID:718035 發(fā)表于 2020-5-26 13:41 | 顯示全部樓層
xuyaqi 發(fā)表于 2020-5-25 21:22
//計(jì)數(shù)0計(jì)數(shù),數(shù)碼管顯示,按鍵接P34
#include
#define uchar unsigned char

多謝您的幫忙
回復(fù)

使用道具 舉報(bào)

ID:870180 發(fā)表于 2021-1-5 20:12 | 顯示全部樓層
/**************************************************************************************
*                              LCD1602電子時(shí)鐘                                                                                                  *
實(shí)現(xiàn)現(xiàn)象:下載程序后,LCD屏上第一行會(huì)顯示出日期,第二行顯示時(shí)間和當(dāng)前溫度。按下K1,進(jìn)入時(shí)間調(diào)節(jié)
                 模式,按下K2讓光標(biāo)往左移,按下K3讓光標(biāo)處的數(shù)字增1,按下K4讓光標(biāo)處的數(shù)字減1
注意事項(xiàng):                                                                                                                                                                  
***************************************************************************************/
//--頭文件聲明,數(shù)據(jù)類型聲明--//
#include <reg52.h>
#include <intrins.h>
typedef unsigned char uint8;
typedef unsigned int uint16;

#define key P3
#define no_key 0xff                                        //無按鍵按下
#define key_state0 0                                //狀態(tài)定義
#define key_state1 1
#define key_state2 2
//---------端口聲明---------//
sbit lcden = P2^7;
sbit lcdrs = P2^6;
sbit lcdrw = P3^6;

sbit key1  = P3^2;
sbit key2  = P3^3;
sbit key3  = P3^4;
sbit key4  = P3^5;

sbit dq    = P3^7;                                        //DS18B20數(shù)據(jù)線
//---------函數(shù)聲明---------//
void lcd_init();                                        //LCD屏幕初始化
void wcom();                                                //LCD寫命令
void wdat();                                                //LCD寫數(shù)據(jù)
void display();                                                //顯示函數(shù)
void timeshow();                                        //時(shí)間顯示
void dateshow();                                        //日期顯示
void weekshow();                                        //星期顯示
void keyscan();                                                //按鍵掃描
void key_disposal();                                //鍵值處理
void day_jud();                                                //日期處理
void ds18b20_init();                                //18B20模塊初始化
uint8 byte_read();                                        //讀去18B20一個(gè)字節(jié)
void byte_write();                                        //寫入18B20一個(gè)字節(jié)
void temp_read();                                        //溫度讀取函數(shù)
void temp_display(uint8 position,uint16 tvalue1);                //溫度顯示函數(shù)
//---------變量定義---------//
uint8 time_1s,time_500ms;
uint8 second,minute = 59,hour = 23;
uint8 day = 1,month = 1,week = 1;                        //設(shè)定初始
uint16 year = 2019,tvalue;
//uint8 temp;
uint8 cursor;                                                                //要調(diào)節(jié)的位置
uint8 key_value;
bit f_twinkle,f_mode,f_pressing,f_set;

uint8 code table1[]={0x10,0x06,0x09,0x08,0x08,0x09,0x06,0x00};                        //字符℃


//---------長(zhǎng)延時(shí)---------//
void delayms(uint16 k){
        uint16 i,j;
        for(i=k;i>0;i--)
                for(j=110;j>0;j--);
}

//---------短延時(shí)---------//
void delayus(uint8 time_1us){
        while(time_1us--);
}

//---------定時(shí)器初始化---------//
void timer_init(){
        EA = 0;
        TR0 = 0;
        TMOD = 0x01;
        TH0 = 0x4C;                //50ms
        TL0 = 0x00;

        ET0 = 1;                //允許Timer0中斷
        ET1 = 1;
        EA  = 1;
        TR0 = 1;
}

//---------Timer0中斷服務(wù)函數(shù)---------//
void isr_timer0 () interrupt 1 using 0 {
        TH0 = 0x4C;                //50ms
        TL0 = 0x00;
        keyscan();
        if(++time_500ms >= 10){
                time_500ms = 0;
                f_twinkle = ~f_twinkle;
        }
        if(!f_set){
                if(++time_1s >= 20){
                        time_1s = 0;
                        second++;
                }
        }
}

void ds18b20_init(){                                                //ds18b20初始化
        dq = 1;       
        _nop_();
        dq = 0;
        delayus(75);
        dq = 1;
        delayus(20);
        dq = 1;
        _nop_();
}

void wcom(uint8 com){                                                //LCD1602寫命令
        lcdrw = 0;
        lcdrs = 0;
        P0 = com;
        lcden = 1;
        delayms(3);
        lcden = 0;
}

void wdat(uint8 dat){                                                //LCD1602寫數(shù)據(jù)
        lcdrw = 0;
        lcdrs = 1;
        P0 = dat;
        lcden = 1;
        delayms(3);
        lcden = 0;       
}

void lcd_init(){                                                        //LCD初始化
        wcom(0x38);
        wcom(0x01);
        wcom(0x06);
        wcom(0x0c);
       
        wcom(0x86);
        wdat(0x2d);                                                                //"-"
        wcom(0x89);
        wdat(0x2d);                                                                //"-"
        wcom(0x80+0x40+0x04);
        wdat(0x3a);                                                                //":"
        wcom(0x80+0x40+0x07);
        wdat(0x3a);                                                                //":"
}

void timeshow(uint8 position,uint8 time){                //時(shí)間顯示,position為顯示位置
        uint8 i,j;
        i = time/10;                                //shiwei
        j = time%10;                                //gewei
        wcom(0x80+0x40+position);
        wdat(0x30+i);                                //shiwei
        wdat(0x30+j);                                //gewei
}

void dateshow(uint8 position,uint8 date1){                //日期顯示(月、日)
        uint8 i,j;
        i = date1/10;                                //shiwei
        j = date1%10;                                //gewei
        wcom(0x80+position);
        wdat(0x30+i);                                //shiwei
        wdat(0x30+j);                                //gewei
}

void yearshow(uint8 position,uint16 year1){                //年份顯示
        uint8 i,j,m,n;
        i = year1/1000;                                //qianwei
        j = year1/100%10;                        //baiwei
        m = year1/10%10;
        n = year1%10;
        wcom(0x80+position);
        wdat(0x30+i);                                //qianwei
        wdat(0x30+j);                                //baiwei
        wdat(0x30+m);                                //shiwei
        wdat(0x30+n);                                //gewei
}

void weekshow(uint8 position,uint8 week1){                //星期顯示
        wcom(0x80+position);
        switch(week1){
                case 1:wdat(0x4d);wdat(0x4f);wdat(0x4e);break;
                case 2:wdat(0x54);wdat(0x55);wdat(0x45);break;
                case 3:wdat(0x57);wdat(0x45);wdat(0x44);break;
                case 4:wdat(0x54);wdat(0x48);wdat(0x55);break;
                case 5:wdat(0x46);wdat(0x52);wdat(0x49);break;
                case 6:wdat(0x53);wdat(0x41);wdat(0x54);break;
                case 7:wdat(0x53);wdat(0x55);wdat(0x4e);break;
                default:break;
        }
}

void day_jud(){                                                                        //日期處理(秒、分、時(shí)、日、月進(jìn)位)
        if(second > 59){
                second = 0;
                if(++minute > 59){
                        minute = 0;
                        if(++hour > 23){
                                hour = 0;
                                if(++week >= 8)        week = 1;
                                day++;
                        }
                }
        }
       
        if(minute > 59){
                minute = 0;
                if(++hour > 23){
                        hour = 0;
                        if(++week > 7)        week = 1;
                        day++;
                }
        }
       
        if(hour > 23){
                hour = 0;
                if(++week > 7)        week = 1;
                day++;
        }
       
        if(day > 31 && (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)){
                day = 1;
                month++;
                if(month >= 12){
                        month = 1;
                        year++;
                }
        }
        else if(day > 30 && (month == 4 || month == 6 || month == 9 || month == 11)){
                day = 1;
                month++;
        }
        else if(day > 29 && month == 2 && ((year%4 == 0 && year%100 != 0) || year%400 == 0)){
                day = 1;
                month++;
        }
        else if(day > 28 && month == 2){
                day = 1;
                month++;
        }
}

void display(){                                                                                //顯示函數(shù)
        if(!f_mode){                                                                        //時(shí)間顯示模式
                yearshow(2,year);
                dateshow(7,month);
                dateshow(0x0a,day);
                weekshow(0x0d,week);
               
                timeshow(2,hour);
                timeshow(5,minute);
                timeshow(0x08,second);
                temp_read();                                                                //讀取溫度
                temp_display(0x0b,tvalue);                                        //溫度顯示
        }
        else{                                                                                        //時(shí)間調(diào)節(jié)模式
                wcom(0x80+0x40+0x0d);
                wdat(0x53);                                                                        //"S"
                wdat(0x45);                                                                        //"E"
                wdat(0x54);                                                                        //"T"

                if(f_twinkle && !f_pressing){                                //閃爍標(biāo)志位,0.5秒取反一次,為1則相應(yīng)位置顯示空白
                        if(cursor == 0) {                                                //按壓標(biāo)志位,按鍵按下時(shí)為1,不閃爍
                                wcom(0x82);                                                        //要顯示空白的位置
                                wdat(0x20);                                                        //4位空白(實(shí)現(xiàn)閃爍效果)
                                wdat(0x20);
                                wdat(0x20);
                                wdat(0x20);
                        }
                        if(cursor == 1) {
                                wcom(0x87);                                                        //要顯示空白的位置
                                wdat(0x20);                                                        //2位空白
                                wdat(0x20);
                        }
                        if(cursor == 2) {
                                wcom(0x8a);
                                wdat(0x20);
                                wdat(0x20);
                        }
                        if(cursor == 3) {
                                wcom(0x8d);
                                wdat(0x20);
                                wdat(0x20);
                                wdat(0x20);
                        }
                        if(cursor == 4) {
                                wcom(0x80+0x42);
                                wdat(0x20);
                                wdat(0x20);
                        }
                        if(cursor == 5) {
                                wcom(0x80+0x45);
                                wdat(0x20);
                                wdat(0x20);
                        }
                        if(cursor == 6) {
                                wcom(0x80+0x48);
                                wdat(0x20);
                                wdat(0x20);
                        }
                }
                else {                                                                                //正常顯示
                        yearshow(2,year);
                        dateshow(7,month);
                        dateshow(0x0a,day);
                        weekshow(0x0d,week);
                        timeshow(2,hour);
                        timeshow(5,minute);
                        timeshow(0x08,second);
                }
        }
}

void keyscan(){                                                                                //按鍵掃描
        static uint8 key_state;
       
        switch(key_state){
                case key_state0:                                                        //未按下
                        if(!key1 || !key2 || !key3 || !key4){
                                key_state = key_state1;
                        }
                break;
                case key_state1:                                                        //有鍵按下
                        f_pressing = 1;
                        if(!key1){
                                f_set = ~f_set;                                                //秒走時(shí)停止
                                f_mode = ~f_mode;                                        //模式切換
                                key_value = 1;
                                key_state = key_state2;
                        }
                        else if(!key2){
                                key_value = 2;
                                key_state = key_state2;
                        }
                        else if(!key3){
                                key_value = 3;
                                key_state = key_state2;
                        }
                        else if(!key4){
                                key_value = 4;
                                key_state = key_state2;
                        }
                        else{
                                key_state = key_state0;
                                f_pressing = 0;                                                //如果不清零,假如按鍵是無效按下,調(diào)節(jié)模式時(shí)就會(huì)停止閃爍
                        }
                break;
                case key_state2:                                                        //松開按鍵
                        if(key1 && key2 && key3 && key4){
                                f_pressing = 0;
                                key_state = key_state0;
                        }
                break;
        }
}

void key_disposal(){                                                                //鍵值處理
        if(!f_pressing){                                                                //松開按鍵才處理
                if(key_value == 1){
                        key_value = 0;
                        cursor = 0;                                                                //光標(biāo)位置清零
                        lcd_init();                                                                //清屏
                }
                else if(key_value == 2){
                        key_value = 0;
                        cursor++;                                                                //光標(biāo)+(光標(biāo)即閃爍的位置,實(shí)際上LCD上并無光標(biāo))
                        if(cursor > 6){
                                cursor = 0;
                        }
                }
                else if((key_value == 3) && f_mode){                //光標(biāo)所處位置值+
                        key_value = 0;
                        if(cursor == 0){
                                year++;
                        }
                        else if (cursor == 1){
                                if(++month > 12) month = 1;
                        }
                        else if(cursor == 2){
                                if(++day > 31) day = 1;
                        }
                        else if(cursor == 3){
                                if(++week > 7) week = 1;
                        }
                        else if(cursor == 4){
                                if(++hour > 23) hour = 0;
                        }
                        else if(cursor == 5){
                                if(++minute > 59) minute = 0;
                        }
                        else if(cursor == 6){
                                if(++second > 59) second = 0;
                        }
                }
                else if((key_value == 4) && f_mode){                //光標(biāo)所處位置值-
                        key_value = 0;
                        if(cursor == 0 && year > 0){
                                year--;
                        }
                        else if (cursor == 1 && month > 1){
                                month--;
                        }
                        else if(cursor == 2 && day > 1){
                                day--;
                        }
                        else if(cursor == 3 && week > 1){
                                week--;
                        }
                        else if(cursor == 4 && hour > 0){
                                hour--;
                        }
                        else if(cursor == 5 && minute > 0){
                                minute--;
                        }
                        else if(cursor == 6 && second > 0){
                                second--;
                        }
                }
        }
}

uint8 byte_read(){                                                                        //ds18b20 讀字節(jié)
        uint8 i,j,dat;
        for(i = 0; i < 8 ; i++){
                dq = 0;
                _nop_();
                dq = 1;
                _nop_();
                j = dq;
                delayus(10);
                dq = 1;
                _nop_();
                dat = (j << 7 | dat >> 1);
        }
        return(dat);
}

void byte_write(uint8 data1){                                                //ds18b20 寫字節(jié)
        uint8 i;
        for(i = 0; i < 8 ; i++){
                dq = 0;
                _nop_();
                dq = data1&0x01;
                delayus(10);
                dq = 1;
                _nop_();
                data1 >>= 1;
        }
}

void temp_read(){                                                                        //讀溫度
        float data1;
        uint8 i,j;
        ds18b20_init();
        byte_write(0xcc);                                                                //向DS18B20發(fā)跳過讀ROM命令
        byte_write(0x44);                                                                //啟動(dòng)DS18B20進(jìn)行溫度轉(zhuǎn)換命令,轉(zhuǎn)換結(jié)果存入內(nèi)部RAM
        delayus(80);
        ds18b20_init();
        byte_write(0xcc);                                                                //向DS18B20發(fā)跳過讀ROM命令
        byte_write(0xbe);                                                                //讀取溫度寄存器等(共可讀9個(gè)寄存器) 前兩個(gè)就是溫度
        delayus(80);
        i = byte_read();                                                                //內(nèi)部RAM 低位
        j = byte_read();                                                                //內(nèi)部RAM 高位
        data1 = (j*256 + i) * 6.25;                                                //取出溫度值
        tvalue = (uint16)data1;                                                        //強(qiáng)制轉(zhuǎn)換
}

void temp_display(uint8 position,uint16 tvalue1){        //溫度顯示
        uint8 i,j,m;
        i = tvalue1/1000;
        j = tvalue1%1000/100;
        m = tvalue1%100/10;                                                                //取出十位 個(gè)位 十分位
        wcom(0x80 + 0x40 + position);
        wdat(0x30 + i);
        wdat(0x30 + j);
        wdat(0x2e);                                                                                //"."
        wdat(0x30 + m);
        wdat(0x00);                                                                                //自編字符
}

void main(){
        uint8 i;
        wcom(0x40);
        for(i = 0; i < 8 ; i++){
                wdat(table1[i]);                                                        //寫入自編字符
        }
       
        timer_init();
        lcd_init();
        while(1){
                // if(++temp > 2){
                        // temp = 0;
                // }
                // switch(temp){
                        // case 0:day_jud();break;
                        // case 1:key_disposal();break;
                        // case 2:display();break;
                // }
                day_jud();
                key_disposal();
                display();
        }
}


仿真出來了,然后連上lcd只有第二行黑格,其他數(shù)字都沒有呀,樓主,咋回事哦。求解,謝謝,
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表