完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
*************************************程序************************************************
#include #include #define uchar unsigned char #define uint unsigned int uchar num1,b,flag; uchar num2,num3,num4; uchar code table[]={ 0x3f,0x06,0x5b,0x4f, 0x66,0x6d,0x7d,0x07, 0x7f,0x6f,0x77,0x7c, 0x39,0x5e,0x79,0x71}; ***it sda=P3^5; ***it scl=P3^6; ***it key=P3^2; ***it key1=P3^3; ***it key2=P3^4; void delay() {;;} void start() //开始信号 { sda=1; delay(); scl=1; delay(); sda=0; delay(); } void stop() //停止 { sda=0; delay(); scl=1; delay(); sda=1; delay(); } void respons() //应答 { uchar i; scl=1; delay(); while((sda==1)&&(i<250))i++; scl=0; delay(); } void init() { sda=1; delay(); scl=1; delay(); SCON=0x50; TMOD=0x20; TL1=0xFa; TH1=0XFa; TR1=1; ES=1; EA=1; num1=44; } void write_byte(uchar date) { uchar i,temp; temp=date; for(i=0;i<8;i++) { temp=temp<<1; scl=0; delay(); sda=CY; delay(); scl=1; delay(); //scl=0; // delay(); } scl=0; delay(); sda=1; delay(); } uchar read_byte() { uchar i,k; scl=0; delay(); sda=1; delay(); for(i=0;i<8;i++) { scl=1; delay(); k=(k<<1)|sda; scl=0; delay(); } return k; } void delay1(uchar x) { uchar a,b; for(a=x;a>0;a--) for(b=100;b>0;b--); } void write_add(uchar address,uchar date) { start(); write_byte(0xa0); respons(); write_byte(address); respons(); write_byte(date); respons(); stop(); } uchar read_add(uchar address) { uchar date; start(); write_byte(0xa0); respons(); write_byte(address); respons(); start(); write_byte(0xa1); respons(); date=read_byte(); stop(); return date; } void display(uchar shi_h,uchar ge_h) { P2=0xfb; P0=table[ge_h]; delay1(5); P2=0xf7; P0=table[shi_h]; delay1(5); shi_h=num1/10; ge_h=num1%10; } void keyscan() { if(key1==0) { delay1(5); if(key1==0) { flag=1; ES=0; SBUF=num1; delay1(500); while(!ti); TI=0; ES=1; while(!key1); } } if(key==0) { delay1(5); } } void main() { init(); num1=read_add(1); write_add(1,num1); while(1) { keyscan(); if(flag==1) display(num1/10,num1%10); } } void ser()interrupt 4 { while(RI!=1); RI=0; b=SBUF; } ***************************************************************************************************** 结果串口发送的num1是在串口在助手显示的是“,” 另外请教前辈们 我要写入三个英文字符进AT24C02而且同样可以用串口发出来的应该怎么写 弄了2天了还是弄不出来。求指教 |
|
相关推荐
7个回答
|
|
|
|
|
|
#include
#include #define uchar unsigned char #define uint unsigned int uchar num1,b,flag; uchar code table_2[]="郑更濠"; uchar code table_1[]="44"; uchar data table_3[2]; uchar data table_4[6]; uchar code table[]={ 0x3f,0x06,0x5b,0x4f, 0x66,0x6d,0x7d,0x07, 0x7f,0x6f,0x77,0x7c, 0x39,0x5e,0x79,0x71}; ***it sda=P3^5; ***it scl=P3^6; ***it key=P3^2; ***it key1=P3^3; ***it key2=P3^4; void delay() {;;} void start() //开始信号 { sda=1; delay(); scl=1; delay(); sda=0; delay(); } void stop() //停止 { sda=0; delay(); scl=1; delay(); sda=1; delay(); } void respons() //应答 { uchar i; scl=1; delay(); while((sda==1)&&(i<250))i++; scl=0; delay(); } void init() { sda=1; delay(); scl=1; delay(); SCON=0x50; TMOD=0x20; TL1=0xFa; TH1=0XFa; TR1=1; ES=1; EA=1; num1=44; } void write_byte(uchar date) { uchar i,temp; temp=date; for(i=0;i<8;i++) { temp=temp<<1; scl=0; delay(); sda=CY; delay(); scl=1; delay(); //scl=0; // delay(); } scl=0; delay(); sda=1; delay(); } uchar read_byte() { uchar i,k; scl=0; delay(); sda=1; delay(); for(i=0;i<8;i++) { scl=1; delay(); k=(k<<1)|sda; scl=0; delay(); } return k; } void delay1(uchar x) { uchar a,b; for(a=x;a>0;a--) for(b=100;b>0;b--); } void write_add(uchar address,uchar date) { start(); write_byte(0xa0); respons(); write_byte(address); respons(); write_byte(date); respons(); stop(); } uchar read_add(uchar address) { uchar date; start(); write_byte(0xa0); respons(); write_byte(address); respons(); start(); write_byte(0xa1); respons(); date=read_byte(); stop(); return date; } void display(uchar shi_h,uchar ge_h) { P2=0xfb; P0=table[ge_h]; delay1(5); P2=0xf7; P0=table[shi_h]; delay1(5); shi_h=num1/10; ge_h=num1%10; } void keyscan() { uchar i; if(key1==0) { delay1(5); if(key1==0) { for(i=0;i<2;i++) { write_add(0x01+i,table_1); flag=1; ES=0; SBUF=table_3; delay1(500); while(!TI); TI=0; } ES=1; while(!key1); delay1(500); while(!key1); } } if(key==0) { delay1(5); if(key==0) { for(i=0;i<6;i++) { write_add(0x05+i,table_2); ES=0; SBUF=table_4; delay1(500); while(!TI); TI=0; } ES=1; while(!key); delay1(500); while(!key); } } } void main() { uchar i,t; init(); for(i=0;i<2;i++) table_3=read_add(0x01+i); num1=atoi(table_3); for(t=0;t<6;t++) table_4[t]=read_add(0x05+t); while(1) { keyscan(); if(flag==1) display(num1/10,num1%10); } } void ser()interrupt 4 { while(RI!=1); RI=0; b=SBUF; } ************************************************************************************************** 这个今晚调出来的 把num1=44换成table_1[]="44"两个字节写进去 然后再把字符串用atoi转换成数字给num1 。程序大部分功能能实现,只不过是名字发到串口助手上还是会有些乱码而已。 |
|
|
|
你串口能正常打印东西啵?
|
|
|
|
对的 可以正常打印
|
|
|
|
仔细看下程序还有什么地方不对吧,IIC这东西时序对了都好办
|
|
|
|
存储芯片好像不支持英文存储吧!你可以吧英文转换成ASCII码(即十六进制)存入24C02,在把ASCII吗读出来写入SBUF发给电脑,电脑串口工具以ASCII码显示
|
|
|
|
|
|
只有小组成员才能发言,加入小组>>
3316 浏览 9 评论
2995 浏览 16 评论
3494 浏览 1 评论
9060 浏览 16 评论
4088 浏览 18 评论
1180浏览 3评论
606浏览 2评论
const uint16_t Tab[10]={0}; const uint16_t *p; p = Tab;//报错是怎么回事?
600浏览 2评论
用NUC131单片机UART3作为打印口,但printf没有输出东西是什么原因?
2335浏览 2评论
NUC980DK61YC启动随机性出现Err-DDR是为什么?
1896浏览 2评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-24 13:08 , Processed in 1.371609 second(s), Total 92, Slave 72 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号