找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2666|回复: 23
收起左侧

单片机自动打铃系统程序 keil报错一大堆

[复制链接]
ID:893765 发表于 2021-3-21 14:50 | 显示全部楼层 |阅读模式
keil报错一大堆 大堆不知问题在哪里的句法错误和明明声明变量了却说没声明 麻烦各位看看
51hei.png
单片机源程序如下:
  1. #include<reg51.h>
  2. #define uchar unsigned char
  3. #define uint unsigned int
  4. #define KSET 0xe0
  5. #define KSET_LONG 0xe1
  6. #define KINC 0xd0
  7. #define KDEC 0xb0
  8. #define KDOWN 0x70

  9. #define AT24C02 0xa0
  10. #define DS1307 0xD0

  11. extern bit ISendStr(uchar sla,uchar suba,uchar*s,uchar no);
  12. extern bit IRcvStr(uchar sla,uchar suba,uchar*s,uchar no);
  13. extern void LCD_DispIni(void);
  14. extern void LCD_DispFill(uchar filldata);
  15. extern void LCD_DispChar(bit color,uchar cy,uchar cx,char dispdata);
  16. extern void LCD_DispStr(bit color,uchar cy,uchar cx,char*disp_str);
  17. extern void LCD_DispHZ(uchar cy,uchar cx,char dispdata);
  18. extern void LCD_DispHZStr(uchar cy,uchar cx,char*disp_str);

  19. sbit SOUND=P3^6;
  20. uchar hzmsg[7]={0,1,2,3,4,5,0xff};
  21. uchar idata alatime[16][2]={{6,00},{6,46},{6,47},{6,48},
  22.                             {10,05},{10,45},{11,40},{13,30},
  23.                             {14,10},{14,50},{15,20},{16,00},
  24.                             {16,40},{17,20},{19,30},{21,00}};
  25. uchar max[3]={24,60,60};
  26. uchar time[3]={6,45,55};
  27. uchar idata sbuf[3];

  28. bit mk;

  29. void delay(uchar t)
  30. {   uchar i;
  31.     while(t--)
  32.     for(i=0;i<250;i++);
  33. }

  34. uchar HexBcd(uchar hex)
  35. {    return(((hex/10)<<4)|(hex%10));
  36. }

  37. uchar BcdHex(uchar bcd)
  38. { uchar hex,hex1;
  39.   hex=bcd&0x0f;
  40.   hex1=(bcd>>4)*10;
  41.   return(hex+hex1);
  42. }

  43. uchar Locate(void)
  44. { uchar n;
  45.   n=0;
  46.   while(n<16)
  47.   {    if(time[0]<alatime[n][0])break;
  48.        else if((time[0]==alatime[n][0])&&(time[1]<alatime[n][1]))break;
  49.        n++;
  50.   }
  51. if(n==16)n=0;
  52. return(n);
  53. }

  54. void LCD_DispDat(bit color,uchar cy,uchar cx,char dat)
  55. { uchar str[3];
  56.   str[0]=dat/10+0x30;
  57.   str[1]=dat%10+0x30;
  58.   str[2]=0;
  59.   LCD_DispStr(color,cy,cx,str);
  60. }

  61. uchar GetKey(void)
  62. { uchar key;
  63.   uchar t;
  64.   if((key=P2&0xf0)==0xf0)return 0xff;
  65.   for(t=0;t<5;t++)delay(5);
  66.   if((key=P2&0xf0)==0xf0)return 0xff;
  67.   while((P2&0xf0)!=0xf0)
  68.   {   delay(5);
  69.       if(t<250)t++;
  70.   }
  71.   if((t>100)&&(key==0xE0))
  72.       return 0xE1;
  73.   return key;
  74. }

  75. void DispTime(void)
  76. {LCD_DispDat(0,2,4,time[0]);
  77. LCD_DispChar(0,2,6,':');
  78. LCD_DispDat(0,2,7,time[1]);
  79. LCD_DispChar(0,2,9,':');
  80. LCD_DispDat(0,2,10,time[2]);
  81. }

  82. void DispAla(bit color,uchar no)
  83. {  LCD_DispDat(color,4+no%4,8,alatime[no][0]);
  84.    LCD_DispChar(color,4+no%4,10,':');
  85.    LCD_DispDat(color,4+no%4,11,alatime[no][1]);
  86. }

  87. void DispPage(uchar page)
  88. {   uchar i;
  89.     for(i=0;i<4;i++)
  90.     {   LCD_DispChar(0,4+i,3,'(');
  91.         LCD_DispDat(0,4+i,4,page*4+i+1);
  92.         LCD_DispChar(0,4+i,6,')');
  93.         DispAla(0,page*4+i);
  94.     }
  95. }

  96. void ReadTime(void)
  97. { IRcvStr(DS1307,0x00,sbuf,3);
  98.   time[0]=BcdHex(sbuf[2]&0x3f);
  99.   time[1]=BcdHex(sbuf[1]&0x7f);
  100.   time[2]=BcdHex(sbuf[0]&0x7f);
  101. }

  102. void SetTime(void)
  103. { sbuf[0]=HexBcd(time[2]);
  104.   sbuf[1]=HexBcd(time[1]);
  105.   sbuf[2]=HexBcd(time[0]);
  106.   ISendStr(DS1307,0x00,sbuf,3);
  107. }

  108. void EditTime(void)
  109. { uchar col=0;
  110.   uchar k;
  111.   ReadTime();
  112.   DispTime();
  113.   LCD_DispDat(1,2,4,time[col]);
  114.   while(1)
  115.   { k=GetKey();
  116.     if(k==KINC)
  117.     {     time[col]++;
  118.           if(time[col]==max[col])
  119.                 time[col]=0;
  120.           LCD_DispDat(1,2,4+col*3,time[col]);
  121.     }
  122.     else if(k==KDEC)
  123.          {   time[col]--;
  124.              if(time[col]==0xff)
  125.                    time[col]=max[col]-1;
  126.              LCD_DispDat(1,2,4+col*3,time[col]);
  127.          }
  128.     else if(k==KSET)
  129.          {   LCD_DispDat(0,2,4+col*3,time[col]);
  130.              col=(col+1)%0x03;
  131.              LCD_DispDat(1,2,4+col*3,time[col]);
  132.          }
  133.     else if(k==KSET_LONG)
  134.          {   LCD_DispDat(0,2,4+col*3,time[col]);
  135.              SetTime();
  136.              break;
  137.          }
  138.   }
  139. }

  140. void EditAla(void)
  141. { uchar no=0;
  142.   uchar col=0;
  143.   uchar k;
  144.   DispPage(0);
  145.   LCD_DispDat(1,4+(no%4),8+col*3,alatime[no][col]);
  146.   while(1)
  147.   {   k=GetKey();
  148.       if(k==KINC)
  149.       {    alatime[no][col]++;
  150.            if(alatime[no][col]==max[col])
  151.                  alatime[no][col]=0;
  152.            LCD_DispDat(1,4+(no%4),8+col*3,alatime[no][col]);
  153.       }
  154.       else if(k==KDEC)
  155.            {   alatime[no][col]--;
  156.                if(alatime[no][col]==0xff)
  157.                   alatime[no][col]=max[col]-1;
  158.                LCD_DispDat(1,4+(no%4),8+col*3,alatime[no][col]);
  159.            }
  160.       else if(k==KSET)
  161.            {   LCD_DispDat(0,4+(no%4),8+col*3,alatime[no][col]);
  162.                col=(col+1)%0x01;
  163.                LCD_DispDat(1,4+(no%4),8+col*3,alatime[no][col]);
  164.            }
  165.       else if(k==KDOWN)
  166.            {   LCD_DispDat(0,4+(no%4),8+col*3,alatime[no][col]);
  167.              no++;
  168.                col=0;
  169.                if(no==16)no=0;
  170.                if(no%4==0)
  171.                   DispPage(no/4);
  172.                LCD_DispDat(1,4+(no%4),8,alatime[no][col]);
  173.            }
  174.       else if(k==KSET_LONG)
  175.            {   for(no=0,no<4;no++)
  176.              {  ISendStr(AT24C02,no*8,&alatime[no*4][0],8);
  177.                 delay(20);
  178.              }
  179.              no=0x55;
  180.              ISendStr(AT24C02,0xff,&no,1);
  181.              break;
  182.            }
  183.   }
  184. }

  185. void int0(void)interrupt 0
  186. {  mk=1;
  187. }

  188. void timer0(void)interrupt 1
  189. { static uchar n=0;
  190.   static uchar m=0;
  191.   TH0=-1000/256;
  192.   TL0=-1000%256;
  193.   n++;
  194.   if(n==200)
  195.   {   n=0;
  196.       m++;
  197.       if(m==100)
  198.       {   m=0;
  199.           TR0=0;
  200.           SOUND=0;
  201.       }
  202.   }
  203.   if(m%4==0)
  204.      SOUND=~SOUND;
  205.   else
  206.      SOUND=1;
  207. }

  208. void main(void)
  209. { uchar i;
  210.   uchar k;
  211.   mk=0;
  212.   TMOD=0x01;
  213.   TH0=-1000/256;
  214.   TL0=-1000%256;
  215.   ET0=1;

  216.   EA=1;
  217.   IT0=1;
  218.   EX0=1;
  219.   LCD_DispIni();
  220.   LCD_DispHZStr(0,1,hzmsg);

  221.   DispTime();
  222.   IRcvStr(AT24C02,0xff,&i,1);
  223.   if(i==0x55)
  224.      IRcvStr(AT24C02,0x00,&alatime[0][0],32);

  225.   SetTime();
  226.   sbuf[0]=0x10;
  227.   ISendStr(DS1307,0x07,sbuf,0x01);
  228.   delay(20);
  229.   i=Locate();

  230.   DispPage(i/4);
  231.   DispAla(1,i);
  232.   while(1)
  233.   { k=GetKey();
  234.     if(k==KSET)
  235.     {   EditTime();
  236.         i=Locate();
  237.         DispPage(i/4);
  238.         DispAla(1,i);
  239.     }
  240.     else if(k==KSET_LONG)
  241.          {  EditAla();
  242.             i=Locate();
  243.             DispPage(i/4);
  244.             DispAla(1,i);
  245.          }
  246.      if(mk==1)
  247.      { ReadTime();
  248.        mk=0;
  249.        DispTime();
  250.            if(time[2]==0)
  251.            {   if((time[1]==alatime[i][1])&&(time[0]==alatime[i][0]))
  252.                {   DispAla(0,i);
  253.                    i++;
  254.                    if(i==16)i=0;
  255.                    if(i%4==0)
  256.                    DispPage(i/4);
  257.                    DispAla(1,i);
  258.                    TR0=1;
  259.                }
  260.            }
  261.      }
  262.   }
  263. }
复制代码

所有资料51hei提供下载:
打铃.rar (25.38 KB, 下载次数: 8)
回复

使用道具 举报

ID:893765 发表于 2021-3-21 16:50 | 显示全部楼层
请问审核员老师在吗 我自己没有传截图和直接公开代码 感谢您 您是天使(确信) 还有您的截图报错情况和我这边的情况不太一样 是自行改代码了吗 方便稍微谈谈您的见解吗
回复

使用道具 举报

ID:213173 发表于 2021-3-21 17:20 | 显示全部楼层
无标题.jpg

评分

参与人数 1黑币 +20 收起 理由
admin + 20 回帖助人的奖励!

查看全部评分

回复

使用道具 举报

ID:213173 发表于 2021-3-21 17:25 | 显示全部楼层
还缺少两个H文件
LCD12864IO.H
I2C.H
回复

使用道具 举报

ID:893765 发表于 2021-3-21 18:03 | 显示全部楼层
wulin 发表于 2021-3-21 17:25
还缺少两个H文件
LCD12864IO.H
I2C.H

谢谢您 请问为什么呢
回复

使用道具 举报

ID:213173 发表于 2021-3-21 20:27 | 显示全部楼层

请了解模块化编程 C语言模块化编程.pdf (1.28 MB, 下载次数: 12)
回复

使用道具 举报

ID:893765 发表于 2021-3-21 22:12 | 显示全部楼层
wulin 发表于 2021-3-21 20:27
请了解模块化编程

我的理解是变量声明与函数正篇的分离 可该报错还是报错啊
回复

使用道具 举报

ID:893765 发表于 2021-3-21 22:12 | 显示全部楼层
wulin 发表于 2021-3-21 20:27
请了解模块化编程

我先试试看
回复

使用道具 举报

ID:893765 发表于 2021-3-21 22:57 | 显示全部楼层
wulin 发表于 2021-3-21 20:27
请了解模块化编程

弄了h文件后I2C和自动打铃系统不报错了 LCD12864IO还是一堆
Build target 'Target 1'
compiling LCD12864IO.c...
LCD12864IO.C(1): warning C500: LICENSE ERROR (R208: RENEW LICENSE ID CODE (LIC))
LCD12864IO.C(1): error C141: syntax error near 'cy', expected ')'
LCD12864IO.C(2): error C141: syntax error near 'code'
LCD12864IO.C(2): error C202: 'pch': undefined identifier
LCD12864IO.C(3): error C141: syntax error near 'i'
LCD12864IO.C(3): error C202: 'i': undefined identifier
LCD12864IO.C(4): error C141: syntax error near 'bit'
LCD12864IO.C(4): error C202: 'port': undefined identifier
LCD12864IO.C(5): error C202: 'cy': undefined identifier
LCD12864IO.C(6): error C202: 'cx': undefined identifier
LCD12864IO.C(7): error C202: 'pch': undefined identifier
LCD12864IO.C(8): error C141: syntax error near 'if'
LCD12864IO.C(9): error C202: 'port': undefined identifier
LCD12864IO.C(10): error C202: 'i': undefined identifier
LCD12864IO.C(13): error C202: 'port': undefined identifier
LCD12864IO.C(14): error C202: 'i': undefined identifier
LCD12864IO.C(16): error C202: 'port': undefined identifier
LCD12864IO.C(17): error C202: 'port': undefined identifier
LCD12864IO.C(18): error C202: 'i': undefined identifier
LCD12864IO.C(20): error C202: 'port': undefined identifier
LCD12864IO.C(22): error C202: 'port': undefined identifier
LCD12864IO.C(23): error C202: 'i': undefined identifier
LCD12864IO.C(25): error C202: 'port': undefined identifier
LCD12864IO.C(27): error C202: 'port': undefined identifier
LCD12864IO.C(28): error C202: 'pch': undefined identifier
LCD12864IO.C(31): error C202: 'port': undefined identifier
LCD12864IO.C(33): error C202: 'port': undefined identifier
LCD12864IO.C(34): error C202: 'i': undefined identifier
LCD12864IO.C(36): error C202: 'port': undefined identifier
LCD12864IO.C(38): error C202: 'port': undefined identifier
LCD12864IO.C(41): error C141: syntax error near 'cy', expected ')'
LCD12864IO.C(42): error C202: 'disp_str': undefined identifier
LCD12864IO.C(43): error C202: 'cy': undefined identifier
LCD12864IO.C(44): error C202: 'cx': undefined identifier
LCD12864IO.C(45): error C202: 'cy': undefined identifier
LCD12864IO.C(46): error C141: syntax error near 'disp_str'
LCD12864IO.C(47): error C202: 'cx': undefined identifier
LCD12864IO.C(48): error C202: 'cx': undefined identifier
LCD12864IO.C(48): error C202: 'cy': undefined identifier
Target not created
回复

使用道具 举报

ID:213173 发表于 2021-3-22 08:00 | 显示全部楼层
田所浩二单推人 发表于 2021-3-21 22:57
弄了h文件后I2C和自动打铃系统不报错了 LCD12864IO还是一堆
Build target 'Target 1'
compiling LCD128 ...

LCD12864IO.C文件内容不完整,端口和变量都没有定义。
回复

使用道具 举报

ID:139866 发表于 2021-3-23 16:48 | 显示全部楼层
你不能直接复制啊,有些接口变量都需要自己定义的

评分

参与人数 1黑币 +20 收起 理由
admin + 20 回帖助人的奖励!

查看全部评分

回复

使用道具 举报

ID:893765 发表于 2021-3-24 18:57 | 显示全部楼层
这些天比较忙才回头再试了试 变成了这样的状况
1.png
回复

使用道具 举报

ID:686782 发表于 2021-3-25 10:44 | 显示全部楼层
田所浩二单推人 发表于 2021-3-24 18:57
这些天比较忙才回头再试了试 变成了这样的状况

首先句尾分号没加语法错误,其次没太看懂这一句是表达什么意思 pch是uchar指针,ACII_TAB明明是uchar却拿来当数组名跟括号

顺便建议看下项目文件结构,别人怎么写的,一个include头文件解决的不用全部extern一大堆,还有extern一般是指明函数的定义在其他的文件,这里根本不需要。
回复

使用道具 举报

ID:893765 发表于 2021-3-25 11:11 | 显示全部楼层
OHHO 发表于 2021-3-25 10:44
首先句尾分号没加语法错误,其次没太看懂这一句是表达什么意思 pch是uchar指针,ACII_TAB明明是uchar却拿 ...

ASCII_TAB那句是指向字符起始列的点阵码 教科书上写的 还有如果不extern就会报错一堆说我没声明变量 笑cry
回复

使用道具 举报

ID:143767 发表于 2021-3-25 12:08 | 显示全部楼层
没有头文件你声明了也没用,依旧会报错
回复

使用道具 举报

ID:686782 发表于 2021-3-25 12:23 | 显示全部楼层
田所浩二单推人 发表于 2021-3-25 11:11
ASCII_TAB那句是指向字符起始列的点阵码 教科书上写的 还有如果不extern就会报错一堆说我没声明变量 笑cr ...

你确认自己代码没错?你的代码里面extern指明的ACSII_TAB根本不是数组地址,还有不是你加了extern不报错就说明这样写很对
我还是那个建议,好好看看别人代码include咋写的。。。不是杠你,可以给你写代码省很多力气。。要不你就一天到晚研究这个那个变量报错没声明吧
回复

使用道具 举报

ID:207421 发表于 2021-3-25 15:00 | 显示全部楼层
1. I2C.c  里面Stop(),调用时用的stop()
2. LCD12864IO.c 里面ASCII_TAB,LCD_WrDat,LCD_WrCmd,  CS1等根本不知从哪里冒来的
3. 自动打铃系统.c 里面ISendStr,IRcvStr,LCD_DispIni,LCD_DispFill,LCD_DispHZ,LCD_DispHZStr这些函数根本不存在,for(no=0,no<4;no++)还写错了
4. 应该学习一下框架式编程
5. 网上拷贝拼凑的代码吧?不然很多函数没有,怎么连自己都不知道

评分

参与人数 1黑币 +20 收起 理由
admin + 20 回帖助人的奖励!

查看全部评分

回复

使用道具 举报

ID:893765 发表于 2021-3-25 18:25 | 显示全部楼层
把缺的东西补了起来 出现了新的问题

新打铃.rar

3.96 KB, 下载次数: 1

回复

使用道具 举报

ID:893765 发表于 2021-3-25 20:29 | 显示全部楼层
lwh999995 发表于 2021-3-25 15:00
1. I2C.c  里面Stop(),调用时用的stop()
2. LCD12864IO.c 里面ASCII_TAB,LCD_WrDat,LCD_WrCmd,  CS1等根 ...

缺的东西都补上了 方便的话请问您可以看一下吗
回复

使用道具 举报

ID:893765 发表于 2021-3-25 20:31 | 显示全部楼层
OHHO 发表于 2021-3-25 12:23
你确认自己代码没错?你的代码里面extern指明的ACSII_TAB根本不是数组地址,还有不是你加了extern不报错 ...

全删掉了 该补的也补上了 感谢您
回复

使用道具 举报

ID:155507 发表于 2021-3-27 08:21 | 显示全部楼层
错误太多:主要是抄写错误,错用大小写等等。
给你改了,对比一下就知道哪里错了。


自动打铃系统.c
  1. #include<reg51.h>
  2. #define uchar unsigned char
  3. #define uint unsigned int
  4. #define KSET 0xe0
  5. #define KSET_LONG 0xe1
  6. #define KINC 0xd0
  7. #define KDEC 0xb0
  8. #define KDOWN 0x70

  9. #define AT24C02 0xa0
  10. #define DS1307 0xD0

  11. extern bit ISendStr(uchar sla,uchar suba,uchar*s,uchar no);
  12. extern bit IRcvStr(uchar sla,uchar suba,uchar*s,uchar no);
  13. extern void LCD_DispIni(void);
  14. extern void LCD_DispFill(uchar filldata);
  15. extern void LCD_DispChar(bit color,uchar cy,uchar cx,char dispdata);
  16. extern void LCD_DispStr(bit color,uchar cy,uchar cx,char*disp_str);
  17. extern void LCD_DispHZ(uchar cy,uchar cx,char dispdata);
  18. extern void LCD_DispHZStr(uchar cy,uchar cx,char*disp_str);

  19. sbit SOUND=P3^6;
  20. uchar hzmsg[7]={0,1,2,3,4,5,0xff};
  21. uchar idata alatime[16][2]={{ 6,00},{ 6,46},{ 6,47},{ 6,48},
  22.                                 {10,05},{10,45},{11,40},{13,30},
  23.                                 {14,10},{14,50},{15,20},{16,00},
  24.                                 {16,40},{17,20},{19,30},{21,00}};
  25. uchar max[3]={24,60,60};
  26. uchar time[3]={6,45,55};
  27. uchar idata sbuf[3];

  28. bit mk;

  29. void delay(uchar t)
  30. {
  31.         uchar i;
  32.         while(t--)
  33.              for(i=0;i<250;i++);
  34. }

  35. uchar HexBcd(uchar hex)
  36. {
  37.     return(((hex/10)<<4)|(hex%10));
  38. }

  39. uchar BcdHex(uchar bcd)
  40. {
  41.         uchar hex,hex1;
  42.         hex=bcd&0x0f;
  43.         hex1=(bcd>>4)*10;
  44.         return(hex+hex1);
  45. }

  46. uchar Locate(void)
  47. {
  48.         uchar n;
  49.         n=0;
  50.         while(n<16)
  51.         {    if(time[0]<alatime[n][0])break;
  52.                 else if((time[0]==alatime[n][0])&&(time[1]<alatime[n][1]))break;
  53.                 n++;
  54.         }
  55.         if(n==16)n=0;
  56.         return(n);
  57. }

  58. void LCD_DispDat(bit color,uchar cy,uchar cx,char dat)
  59. {
  60.         uchar str[3];
  61.         str[0]=dat/10+0x30;
  62.         str[1]=dat%10+0x30;
  63.         str[2]=0;
  64.         LCD_DispStr(color,cy,cx,str);
  65. }

  66. uchar GetKey(void)
  67. {
  68.         uchar key;
  69.         uchar t;
  70.         if((key=P2&0xf0)==0xf0)return 0xff;
  71.         for(t=0;t<5;t++)delay(5);
  72.         if((key=P2&0xf0)==0xf0)return 0xff;
  73.         while((P2&0xf0)!=0xf0)
  74.         {   delay(5);
  75.                 if(t<250)t++;
  76.         }
  77.         if((t>100)&&(key==0xE0))
  78.         return 0xE1;
  79.         return key;
  80. }

  81. void DispTime(void)
  82. {
  83.         LCD_DispDat(0,2,4,time[0]);
  84.         LCD_DispChar(0,2,6,':');
  85.         LCD_DispDat(0,2,7,time[1]);
  86.         LCD_DispChar(0,2,9,':');
  87.         LCD_DispDat(0,2,10,time[2]);
  88. }

  89. void DispAla(bit color,uchar no)
  90. {
  91.         LCD_DispDat(color,4+no%4,8,alatime[no][0]);
  92.         LCD_DispChar(color,4+no%4,10,':');
  93.         LCD_DispDat(color,4+no%4,11,alatime[no][1]);
  94. }

  95. void DispPage(uchar page)
  96. {
  97.         uchar i;
  98.         for(i=0;i<4;i++)
  99.         {   LCD_DispChar(0,4+i,3,'(');
  100.                 LCD_DispDat(0,4+i,4,page*4+i+1);
  101.                 LCD_DispChar(0,4+i,6,')');
  102.                 DispAla(0,page*4+i);
  103.         }
  104. }

  105. void ReadTime(void)
  106. {
  107.         IRcvStr(DS1307,0x00,sbuf,3);
  108.         time[0]=BcdHex(sbuf[2]&0x3f);
  109.         time[1]=BcdHex(sbuf[1]&0x7f);
  110.         time[2]=BcdHex(sbuf[0]&0x7f);
  111. }

  112. void SetTime(void)
  113. {
  114.         sbuf[0]=HexBcd(time[2]);
  115.         sbuf[1]=HexBcd(time[1]);
  116.         sbuf[2]=HexBcd(time[0]);
  117.         ISendStr(DS1307,0x00,sbuf,3);
  118. }

  119. void EditTime(void)
  120. { uchar col=0;
  121.         uchar k;
  122.         ReadTime();
  123.         DispTime();
  124.         LCD_DispDat(1,2,4,time[col]);
  125.         while(1)
  126.         { k=GetKey();
  127.                 if(k==KINC)
  128.                 {     time[col]++;
  129.                         if(time[col]==max[col])
  130.                         time[col]=0;
  131.                         LCD_DispDat(1,2,4+col*3,time[col]);
  132.                 }
  133.                 else if(k==KDEC)
  134.                 {   time[col]--;
  135.                         if(time[col]==0xff)
  136.                         time[col]=max[col]-1;
  137.                         LCD_DispDat(1,2,4+col*3,time[col]);
  138.                 }
  139.                 else if(k==KSET)
  140.                 {   LCD_DispDat(0,2,4+col*3,time[col]);
  141.                         col=(col+1)%0x03;
  142.                         LCD_DispDat(1,2,4+col*3,time[col]);
  143.                 }
  144.                 else if(k==KSET_LONG)
  145.                 {   LCD_DispDat(0,2,4+col*3,time[col]);
  146.                         SetTime();
  147.                         break;
  148.                 }
  149.         }
  150. }

  151. void EditAla(void)
  152. { uchar no=0;
  153.         uchar col=0;
  154.         uchar k;
  155.         DispPage(0);
  156.         LCD_DispDat(1,4+(no%4),8+col*3,alatime[no][col]);
  157.         while(1)
  158.         {   k=GetKey();
  159.                 if(k==KINC)
  160.                 {    alatime[no][col]++;
  161.                         if(alatime[no][col]==max[col])
  162.                         alatime[no][col]=0;
  163.                         LCD_DispDat(1,4+(no%4),8+col*3,alatime[no][col]);
  164.                 }
  165.                 else if(k==KDEC)
  166.                 {   alatime[no][col]--;
  167.                         if(alatime[no][col]==0xff)
  168.                         alatime[no][col]=max[col]-1;
  169.                         LCD_DispDat(1,4+(no%4),8+col*3,alatime[no][col]);
  170.                 }
  171.                 else if(k==KSET)
  172.                 {   LCD_DispDat(0,4+(no%4),8+col*3,alatime[no][col]);
  173.                         col=(col+1)%0x01;
  174.                         LCD_DispDat(1,4+(no%4),8+col*3,alatime[no][col]);
  175.                 }
  176.                 else if(k==KDOWN)
  177.                 {   LCD_DispDat(0,4+(no%4),8+col*3,alatime[no][col]);
  178.                         no++;
  179.                         col=0;
  180.                         if(no==16)no=0;
  181.                         if(no%4==0)
  182.                         DispPage(no/4);
  183.                         LCD_DispDat(1,4+(no%4),8,alatime[no][col]);
  184.                 }
  185.                 else if(k==KSET_LONG)
  186.                 {   for(no=0;no<4;no++)
  187.                         {  ISendStr(AT24C02,no*8,&alatime[no*4][0],8);
  188.                                 delay(20);
  189.                         }
  190.                         no=0x55;
  191.                         ISendStr(AT24C02,0xff,&no,1);
  192.                         break;
  193.                 }
  194.         }
  195. }

  196. void main(void)
  197. {
  198.         uchar i;
  199.         uchar k;
  200.         mk=0;
  201.         TMOD=0x01;
  202.         TH0=-1000/256;
  203.         TL0=-1000%256;
  204.         ET0=1;

  205.         EA=1;
  206.         IT0=1;
  207.         EX0=1;
  208.         LCD_DispIni();
  209.         LCD_DispHZStr(0,1,hzmsg);

  210.         DispTime();
  211.         IRcvStr(AT24C02,0xff,&i,1);
  212.         if(i==0x55)
  213.         IRcvStr(AT24C02,0x00,&alatime[0][0],32);

  214.         SetTime();
  215.         sbuf[0]=0x10;
  216.         ISendStr(DS1307,0x07,sbuf,0x01);
  217.         delay(20);
  218.         i=Locate();

  219.         DispPage(i/4);
  220.         DispAla(1,i);
  221.         while(1)
  222.         {
  223.                 k=GetKey();
  224.                 if(k==KSET)
  225.                 {   EditTime();
  226.                         i=Locate();
  227.                         DispPage(i/4);
  228.                         DispAla(1,i);
  229.                 }
  230.                 else if(k==KSET_LONG)
  231.                 {  EditAla();
  232.                         i=Locate();
  233.                         DispPage(i/4);
  234.                         DispAla(1,i);
  235.                 }
  236.                 if(mk==1)
  237.                 { ReadTime();
  238.                         mk=0;
  239.                         DispTime();
  240.                         if(time[2]==0)
  241.                         {   if((time[1]==alatime[i][1])&&(time[0]==alatime[i][0]))
  242.                                 {   DispAla(0,i);
  243.                                         i++;
  244.                                         if(i==16)i=0;
  245.                                         if(i%4==0)
  246.                                         DispPage(i/4);
  247.                                         DispAla(1,i);
  248.                                         TR0=1;
  249.                                 }
  250.                         }
  251.                 }
  252.         }
  253. }


  254. void int0(void)interrupt 0
  255. {
  256.         mk=1;
  257. }

  258. void timer0(void)interrupt 1
  259. {
  260.         static uchar n=0;
  261.         static uchar m=0;
  262.         TH0=-1000/256;
  263.         TL0=-1000%256;
  264.         n++;
  265.         if(n==200)
  266.         {   n=0;
  267.                 m++;
  268.                 if(m==100)
  269.                 {   m=0;
  270.                         TR0=0;
  271.                         SOUND=0;
  272.                 }
  273.         }
  274.         if(m%4==0)
  275.         SOUND=~SOUND;
  276.         else
  277.         SOUND=1;
  278. }

复制代码

LCD12864IO.c
  1. #include <reg51.h>
  2. #include <absacc.h>
  3. #define uchar unsigned char
  4. #define uint unsigned int
  5. sbit LCD_RST=P1^0;
  6. sbit DI=P1^5;
  7. sbit RW=P1^4;
  8. sbit CS1=P1^2;
  9. sbit CS2=P1^1;
  10. sbit EN=P1^3;
  11. sbit RDY=P0^7;
  12. sfr LCD=0x80;
  13. #define LCD_DISPON 0x3f
  14. #define LCD_STARTROW 0xc0
  15. #define LCD_ADDRSTRY 0xb8
  16. #define LCD_ADDRSTRX 0x40
  17. #define P_CS1 0
  18. #define P_CS2 1

  19. uchar code  ASCII_TAB[480] =
  20. { 0x00, 0x00, 0x00, 0x00, 0x00,
  21.         0x00, 0x00, 0x9e, 0x00, 0x00,
  22.         0x00, 0x0e, 0x00, 0x0e, 0x00,
  23.         0x28, 0xfe, 0x28, 0xfe, 0x28,
  24.         0x48, 0x54, 0xfe, 0x54, 0x24,
  25.         0x46, 0x26, 0x10, 0xc8, 0xc4,
  26.         0x6c, 0x92, 0xaa, 0x44, 0xa0,
  27.         0x00, 0x0a, 0x06, 0x00, 0x00,
  28.         0x00, 0x38, 0x44, 0x82, 0x00,
  29.         0x00, 0x82, 0x44, 0x38, 0x00,
  30.         0x28, 0x10, 0x7c, 0x10, 0x28,
  31.         0x10, 0x10, 0x7c, 0x10, 0x10,
  32.         0x00, 0xa0, 0x60, 0x00, 0x00,
  33.         0x10, 0x10, 0x10, 0x10, 0x10,
  34.         0x00, 0xc0, 0xc0, 0x00, 0x00,
  35.         0x40, 0x20, 0x10, 0x08, 0x04,
  36.         0x7C, 0xA2, 0x92, 0x8A, 0x7C,  
  37.         0x84, 0xC2, 0xA2, 0x92, 0x8C,  
  38.         0x42, 0x82, 0x8A, 0x96, 0x62,  
  39.         0x30, 0x28, 0x24, 0xFE, 0x20,
  40.         0x4E, 0x8A, 0x8A, 0x8A, 0x72,  
  41.         0x78, 0x94, 0x92, 0x92, 0x60,   
  42.         0x02, 0xE2, 0x12, 0x0A, 0x06,   
  43.         0x6C, 0x92, 0x92, 0x92, 0x6C,   
  44.         0x0C, 0x92, 0x92, 0x52, 0x3C,   
  45.         0x00, 0x6C, 0x6C, 0x00, 0x00,  
  46.         0x00, 0xAC, 0x6C, 0x00, 0x00,   
  47.         0x10, 0x28, 0x44, 0x82, 0x00,   
  48.         0x28, 0x28, 0x28, 0x28, 0x28,   
  49.         0x00, 0x82, 0x44, 0x28, 0x10,   
  50.         0x04, 0x02, 0xA2, 0x12, 0x0C,
  51.         0x64, 0x92, 0xF2, 0x82, 0x7C,  
  52.         0xFC, 0x22, 0x22, 0x22, 0xFC,   
  53.         0xFE, 0x92, 0x92, 0x92, 0x6C,   
  54.         0x7C, 0x82, 0x82, 0x82, 0x44,   
  55.         0xFE, 0x82, 0x82, 0x44, 0x38,  
  56.         0xFE, 0x92, 0x92, 0x92, 0x82,  
  57.         0xFE, 0x12, 0x12, 0x12, 0x02,   
  58.         0x7C, 0x82, 0x92, 0x92, 0xF4,   
  59.         0xFE, 0x10, 0x10, 0x10, 0xFE,  
  60.         0x00, 0x82, 0xFE, 0x82, 0x00,  
  61.         0x40, 0x80, 0x82, 0x7E, 0x02,   
  62.         0xFE, 0x10, 0x28, 0x44, 0x82,   
  63.         0xFE, 0x80, 0x80, 0x80, 0x80,  
  64.         0xFE, 0x04, 0x18, 0x04, 0xFE,   
  65.         0xFE, 0x08, 0x10, 0x20, 0xFE,   
  66.         0x7C, 0x82, 0x82, 0x82, 0x7C,
  67.         0xFE, 0x12, 0x12, 0x12, 0x0C,
  68.         0x7C, 0x82, 0xA2, 0x42, 0xBC,
  69.         0xFE, 0x12, 0x32, 0x52, 0x8C,  
  70.         0x8C, 0x92, 0x92, 0x92, 0x62,
  71.         0x02, 0x02, 0xFE, 0x02, 0x02,
  72.         0x7E, 0x80, 0x80, 0x80, 0x7E,
  73.         0x3E, 0x40, 0x80, 0x40, 0x3E,
  74.         0x7E, 0x80, 0x70, 0x80, 0x7E,
  75.         0xC6, 0x28, 0x10, 0x28, 0xC6,
  76.         0x0E, 0x10, 0xE0, 0x10, 0x0E,
  77.         0xC2, 0xA2, 0x92, 0x8A, 0x86,
  78.         0x00, 0xFE, 0x82, 0x82, 0x00,
  79.         0x04, 0x08, 0x10, 0x20, 0x40,
  80.         0x00, 0x82, 0x82, 0xFE, 0x00,
  81.         0x08, 0x04, 0x02, 0x04, 0x08,
  82.         0x80, 0x80, 0x80, 0x80, 0x80,
  83.         0x00, 0x02, 0x04, 0x08, 0x00,
  84.         0x40, 0xA8, 0xA8, 0xA8, 0xF0,
  85.         0xFE, 0x90, 0x88, 0x88, 0x70,
  86.         0x70, 0x88, 0x88, 0x88, 0x40,
  87.         0x70, 0x88, 0x88, 0x90, 0xFE,
  88.         0x70, 0xA8, 0xA8, 0xA8, 0x30,
  89.         0x10, 0xFC, 0x12, 0x02, 0x04,
  90.         0x18, 0xA4, 0xA4, 0xA4, 0x7C,
  91.         0xFE, 0x10, 0x08, 0x08, 0xF0,
  92.         0x00, 0x88, 0xFA, 0x80, 0x00,
  93.         0x40, 0x80, 0x88, 0x7A, 0x00,
  94.         0xFE, 0x20, 0x50, 0x88, 0x00,
  95.         0x00, 0x82, 0xFE, 0x80, 0x00,
  96.         0xF8, 0x08, 0x30, 0x08, 0xF8,
  97.         0xF8, 0x10, 0x08, 0x08, 0xF0,
  98.         0x70, 0x88, 0x88, 0x88, 0x70,
  99.         0xF8, 0x28, 0x28, 0x28, 0x10,
  100.         0x10, 0x28, 0x28, 0x30, 0xF8,
  101.         0xF8, 0x10, 0x08, 0x08, 0x10,
  102.         0x90, 0xA8, 0xA8, 0xA8, 0x40,
  103.         0x08, 0x7E, 0x88, 0x80, 0x40,
  104.         0x78, 0x80, 0x80, 0x40, 0xF8,
  105.         0x38, 0x40, 0x80, 0x40, 0x38,
  106.         0x78, 0x80, 0x60, 0x80, 0x78,
  107.         0x88, 0x50, 0x20, 0x50, 0x88,
  108.         0x18, 0xA0, 0xA0, 0xA0, 0x78,
  109.         0x88, 0xC8, 0xA8, 0x98, 0x88,
  110.         0x00, 0x10, 0x6C, 0x82, 0x00,
  111.         0x00, 0x00, 0xFE, 0x00, 0x00,
  112.         0x00, 0x82, 0x6C, 0x10, 0x00,
  113.         0x10, 0x10, 0x54, 0x38, 0x10,
  114.         0x10, 0x38, 0x54, 0x10, 0x10};

  115.         uchar code  HZTAB[480] = {0};  //?????

  116. void LCD_WrCmd(bit port,uchar cmd)
  117. {  EN=0;
  118.         if(port)
  119.         {
  120.                 CS2=1;
  121.                 CS1=0;
  122.         }
  123.         else
  124.         {
  125.                 CS2=0;
  126.                 CS1=1;
  127.         }
  128.         DI=0;
  129.         RW=0;
  130.         EN=1;
  131.         LCD=cmd;
  132.         EN=0;
  133. }

  134. void LCD_WrDat(bit port,uchar wrdata)
  135. {  EN=0;
  136.         if(port)
  137.         {
  138.                 CS2=1;
  139.                 CS1=0;
  140.         }
  141.         else
  142.         {
  143.                 CS2=0;
  144.                 CS1=1;
  145.         }
  146.         DI=1;
  147.         RW=0;
  148.         EN=1;
  149.         LCD=wrdata;
  150.         EN=0;
  151. }


  152. void LCD_DispFill(uchar filldata)
  153. {  uchar x,y;
  154.         LCD_WrCmd(P_CS1,LCD_STARTROW);
  155.         LCD_WrCmd(P_CS2,LCD_STARTROW);
  156.         for(y=0;y<8;y++)
  157.         {   LCD_WrCmd(P_CS1,LCD_ADDRSTRY+y);
  158.                 LCD_WrCmd(P_CS1,LCD_ADDRSTRX);
  159.                 LCD_WrCmd(P_CS2,LCD_ADDRSTRY+y);
  160.                 LCD_WrCmd(P_CS2,LCD_ADDRSTRX);
  161.                 for(x=0;x<64;x++)
  162.                 {
  163.                     LCD_WrDat(P_CS1,filldata);
  164.                         LCD_WrDat(P_CS2,filldata);
  165.                 }
  166.         }
  167. }

  168. void LCD_DispIni(void)
  169. {  uint i;
  170.         LCD_RST=0;
  171.         for(i=0;i<500;i++);
  172.         LCD_RST=1;
  173.         LCD_WrCmd(P_CS1,LCD_DISPON);
  174.         LCD_WrCmd(P_CS1,LCD_STARTROW);
  175.         LCD_WrCmd(P_CS2,LCD_DISPON);
  176.         LCD_WrCmd(P_CS2,LCD_STARTROW);
  177.         LCD_DispFill(00);
  178.         LCD_WrCmd(P_CS1,LCD_ADDRSTRY+0);
  179.         LCD_WrCmd(P_CS1,LCD_ADDRSTRX+0);
  180.         LCD_WrCmd(P_CS2,LCD_ADDRSTRY+0);
  181.         LCD_WrCmd(P_CS2,LCD_ADDRSTRX+0);
  182. }

  183. void LCD_DispChar(bit color,uchar cy,uchar cx,char dispdata)
  184. {   uchar *pch;
  185.         uchar i;
  186.         bit port;
  187.         cy=cy&0x07;
  188.         cx=cx&0x0f;
  189.         pch=&ASCII_TAB[(dispdata-0X20)*5];
  190.         if((cx&0x08)==0)
  191.         {
  192.                 port=P_CS1;
  193.                 i=cx<<3;
  194.         }
  195.         else
  196.         {  port=P_CS2;
  197.                 i=(cx&0x07)<<3;
  198.         }
  199.         LCD_WrCmd(port,LCD_ADDRSTRX+i);
  200.         LCD_WrCmd(port,LCD_ADDRSTRY+cy);
  201.         for(i=0;i<5;i++);
  202.         if(color==0)
  203.         LCD_WrDat(port,0x00);
  204.         else
  205.         LCD_WrDat(port,0xff);
  206.         for(i=0;i<5;i++)
  207.         {if(color==0)
  208.                 LCD_WrDat(port,*pch);
  209.                 else
  210.                 LCD_WrDat(port,~*pch);
  211.                 pch++;
  212.         }
  213.         if(color==0)
  214.         LCD_WrDat(port,0x00);
  215.         else
  216.         LCD_WrDat(port,0xff);
  217.         for(i=0;i<5;i++)
  218.         if(color==0)
  219.         LCD_WrDat(port,0x00);
  220.         else
  221.         LCD_WrDat(port,0xff);
  222. }

  223. void LCD_DispStr(bit color,uchar cy,uchar cx,char*disp_str)
  224. {  while(*disp_str!='\0')
  225.         {
  226.                 cy=cy&0x07;
  227.                 cx=cx&0x0f;
  228.                 LCD_DispChar(color,cy,cx,*disp_str);
  229.                 disp_str++;
  230.                 cx++;
  231.                 if(cx>15)cy++;
  232.         }
  233. }

  234. void LCD_DispHZ(uchar cy, uchar cx, char dispdata)
  235. {
  236.   uchar *pdat;
  237.         uchar i,s,page;
  238.         bit port;
  239.         cy=cy&0x03;
  240.         cx=cx&0x07;
  241.         pdat=&HZTAB[dispdata*32];
  242.         if((cx&0x04)==0)
  243.         {
  244.                 port=0;
  245.                 s=cx<<4;
  246.         }
  247.         else
  248.         {
  249.                 port=1;
  250.                 s=(cx<<4)-64;
  251.         }
  252.         for(page=0;page<2;page++)
  253.         { LCD_WrCmd(port,LCD_ADDRSTRX+s);
  254.                 LCD_WrCmd(port,LCD_ADDRSTRY+(cy<<1)+page);
  255.                 for(i=0;i<5;i++);
  256.                 for(i=0;i<16;i++)
  257.                 { LCD_WrDat(port,*pdat);
  258.                         pdat++;
  259.                 }
  260.                 for(i=0;i<5;i++);
  261.         }
  262. }

  263. void LCD_DispHZStr(uchar cy,uchar cx,char*disp_str)
  264. {   while(*disp_str!=0xff)
  265.         {
  266.                 cy=cy&0x03;
  267.                 cx=cx&0x07;
  268.                 LCD_DispHZ(cy,cx,*disp_str);
  269.                 disp_str++;
  270.                 cx++;
  271.                 if(cx>7)
  272.                 {cy++;
  273.                         cx=0;
  274.                 }
  275.         }
  276. }
复制代码

I2C.c
  1. #include <reg51.h>
  2. #include <intrins.h>
  3. #define uchar unsigned char
  4. #define delay1us() _nop_()
  5. #define delay5us() _nop_();_nop_();_nop_();_nop_();_nop_()
  6. sbit SDA=P3^5;
  7. sbit SCL=P3^4;
  8. bit ack_mk;

  9. void Start()
  10. {
  11.     SDA=1;
  12.     SCL=1;
  13.     delay5us();
  14.     SDA=0;
  15.     delay5us();
  16.     SCL=0;
  17. }

  18. void Stop()
  19. {
  20.           SDA=0;
  21.     SCL=1;
  22.     delay5us();
  23.     SDA=1;
  24.     delay5us();
  25.     SCL=0;
  26. }

  27. void Ack(void)
  28. {
  29.           SDA=0;
  30.     SCL=1;
  31.     delay5us();
  32.     SCL=0;
  33. }

  34. void NAck(void)
  35. {   SDA=1;
  36.     SCL=1;
  37.     delay5us();
  38.     SCL=0;
  39. }

  40. void SendByte(uchar c)
  41. {    uchar n;
  42.      for(n=0;n<8;n++)
  43.      {
  44.                 if(c&0x80)
  45.         SDA=1;
  46.         else
  47.             SDA=0;
  48.         SCL=1;
  49.         delay5us();
  50.         SCL=0;
  51.         c=c<<1;
  52.      }
  53.      delay5us();
  54.      SDA=1;
  55.      delay5us();
  56.      SCL=1;
  57.      delay5us();
  58.      if(SDA==1)
  59.          ack_mk=0;
  60.      else
  61.          ack_mk=1;
  62.      SCL=0;
  63. }

  64. uchar RcvByte()
  65. {
  66.   uchar c;
  67.   uchar n;
  68.   for(n=0;n<8;n++)
  69.   {
  70.         SDA=1;
  71.     SCL=1;
  72.     if(SDA==0)
  73.         c=c&0x7f;
  74.     else
  75.         c=c|0x80;
  76.     c=_crol_(c,1);
  77.     SCL=0;
  78.   }
  79.   return(c);
  80. }

  81. bit ISendStr(uchar sla, uchar suba, uchar*s, uchar n)
  82. {  uchar i;
  83.    Start();
  84.    SendByte(sla);
  85.     if(ack_mk==0)return(0);
  86.    SendByte(suba);
  87.     if(ack_mk==0)return(0);
  88.    for(i=0;i<n;i++)
  89.    {   SendByte(*s);
  90.        if(ack_mk==0)return(0);
  91.        s++;
  92.    }
  93.    Stop();
  94.    return(1);
  95. }

  96. bit IRcvStr(uchar sla, uchar suba, uchar*s, uchar n)
  97. {
  98.    uchar i;
  99.    Start();
  100.    SendByte(sla);
  101.     if(ack_mk==0)return(0);
  102.    SendByte(suba);
  103.     if(ack_mk==0)return(0);
  104.    Start();
  105.    SendByte(sla+1);
  106.       if(ack_mk==0)return(0);
  107.    for(i=0;i<n-1;i++)
  108.    {  *s=RcvByte();
  109.       Ack();
  110.       s++;
  111.    }
  112.    *s=RcvByte();
  113.    NAck();
  114.    Stop();
  115.    return(1);
  116. }
复制代码

回复

使用道具 举报

ID:893765 发表于 2021-3-27 14:37 | 显示全部楼层
angmall 发表于 2021-3-27 08:21
错误太多:主要是抄写错误,错用大小写等等。
给你改了,对比一下就知道哪里错了。

您好 新帖里的新代码已经补上所有缺的东西了 请问P_CS1的P的意思是?
回复

使用道具 举报

ID:155507 发表于 2021-3-27 16:06 | 显示全部楼层
田所浩二单推人 发表于 2021-3-27 14:37
您好 新帖里的新代码已经补上所有缺的东西了 请问P_CS1的P的意思是?

在c语言中不能有两个同名变量

前面 sbit CS1=P1^2; 已经有一个CS1,所以这里 #define P_CS1 0 不能有相同的
回复

使用道具 举报

ID:893765 发表于 2021-3-27 16:22 | 显示全部楼层
angmall 发表于 2021-3-27 16:06
在c语言中不能有两个同名变量

前面 sbit CS1=P1^2; 已经有一个CS1,所以这里 #define P_CS1 0 不能有 ...

谢谢您
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表