| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 5754 人关注过本帖
标题:ID卡读/写卡程序,读的部分很成功,写的部分不成功,应该如何编程?
只看楼主 加入收藏
h1357951
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2017-5-2
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:3 
ID卡读/写卡程序,读的部分很成功,写的部分不成功,应该如何编程?
//    ID卡读/写卡程序,读的部分很成功,写的部分不成功,应该如何编程?
#include <reg52.h>                                    //包含头文件
#define uchar unsigned char                            //宏定义
#define uint unsigned int                            //宏定义
#define LCD_data  P0                                 //数据口
sbit int0=P3^0;                                        //串口读入
sbit int1=P3^1;                                        //串口写出
sbit dxzk=P1^4;                                        //串口读/写转换开关
sbit LCD_RS  =  P1^0;            //数据/地址寄存器选择输入
sbit LCD_RW  =  P1^1;            //液晶读/写控制
sbit LCD_EN  =  P1^2;            //液晶使能控制
sbit LCD_PSB =  P2^7;            //串/并方式控制
sbit LCD_RST  =  P2^6;              //    lcd复位端
char a[13];                            //字符数组a,ID卡的十六进制卡号,a[13]为系统自动增设的终结符(\0)
char b[13];                            //字符数组b,要写入ID卡的十六进制卡号
         
         
         
         
 void main()
     {
        init();                                          //调用初始化子函数
        while(1)
            {   
                if(flag==1)   
                    {                                        //读取卡号
                        flag=0;                                //清除flag标志位
                          ES=0;                        //关串口中断
                        lcd_wcmd(0x01);              //清除LCD的显示内容
                        delay(5);                                       
                        if(dxzk=='1')                        //若串口读/写转换开关为高电平
                            {
                                                                                          
                                i=0;                        // i清零
                                lcd_pos(0,0);               //设置显示位置为第一行的第1个字符
                                while(a[i]!= '\0')
                                    {
                                        lcd_wdat(a[i]);        //逐个元素写卡号到显示屏
                                        i++    ;               
                                    }
                            }
                         else                                 //若串口读/写转换开关为低电平
                             {                                 //向卡写卡号
                                i=0;                         // i清零
                                lcd_pos(0,0);               //设置显示位置为第一行的第1个字符
                                while(b[i]!= '\0')            
                                    {
                                        lcd_wdat(b[i]);          //逐个元素写卡号到显示屏
                                        i++    ;               
                                    }
                                       
                            }
                     }
             }
    }
void ser() interrupt 4                                //串口中断程序
    {
        ES=0;                                        //关串口中断
        RI=0;                                        //清零串口接收标志位
        if(flag==1)
            {
                for ( i = 0; i < 13; i++)                    //在c语言中,数组是不能整体赋值的,即a[13]=SBUF是错误的,必须是逐个元素赋值才行
                                                            //读取卡号
                    {
                        a[i] = SBUF;                        
                        while(!RI);
                        RI=0;
                    }
                flag=1;
                ES=1;                                        //开串口中断
            }
        else
            {
                for ( i = 0; i < 13; i++)                    //将数组b[]逐个写入ID卡中(即十六进制卡号)
                    {
                        SBUF=b[i];
                        while(!TI);
                        TI=0;
            }
        flag=1;
        ES=1;                                        //开串口中断
    }   
搜索更多相关主题的帖子: 成功 串口 flag 中断 显示 
2017-10-11 17:10
grmmylbs
Rank: 14Rank: 14Rank: 14Rank: 14
等 级:贵宾
威 望:54
帖 子:1409
专家分:5845
注 册:2016-2-14
收藏
得分:20 
flag置位有问题吧?你应该想清楚什么情况下flag置1,什么时候置0
2017-10-11 17:28
h1357951
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2017-5-2
收藏
得分:0 
flag置位没有问题
2017-10-11 17:38
h1357951
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2017-5-2
收藏
得分:0 
这样改动可行吗?
#include <reg52.h>                                    //包含头文件
#define uchar unsigned char                            //宏定义
#define uint unsigned int                            //宏定义
#define LCD_data  P0                                 //数据口
sbit int0=P3^0;                                        //串口读入
sbit int1=P3^1;                                        //串口写出
sbit dxzk=P1^4;                                        //串口读/写转换开关
sbit LCD_RS  =  P1^0;            //数据/地址寄存器选择输入
sbit LCD_RW  =  P1^1;            //液晶读/写控制
sbit LCD_EN  =  P1^2;            //液晶使能控制
sbit LCD_PSB =  P2^7;            //串/并方式控制
sbit LCD_RST  =  P2^6;              //    lcd复位端
char a[13];                            //字符数组a,ID卡的十六进制卡号,a[13]为系统自动增设的终结符(\0)
char b[13];                            //字符数组b,要写入ID卡的十六进制卡号
         
         
         
         
 void main()
     {
        init();                                          //调用初始化子函数
        while(1)
            {   
                if(flag==1)   
                    {                                        //读取卡号
                        flag=0;                                //清除flag标志位
                          ES=0;                        //关串口中断
                        lcd_wcmd(0x01);              //清除LCD的显示内容
                        delay(5);                                       
                        if(dxzk=='1')                        //若串口读/写转换开关为高电平
                            {
                                                                                          
                                i=0;                        // i清零
                                lcd_pos(0,0);               //设置显示位置为第一行的第1个字符
                                while(a[i]!= '\0')
                                    {
                                        lcd_wdat(a[i]);        //逐个元素写卡号到显示屏
                                        i++    ;               
                                    }
                            }
                         else                                 //若串口读/写转换开关为低电平
                             {                                 //向卡写卡号
                                for ( i = 0; i < 13; i++)                    //将数组b[]的逐个元素写入ID卡中(即十六进制卡号)
                                    {
                                        SBUF=b[i];
                                        while(!TI);
                                        TI=0;
                                     }
                            }
                     }
             }
    }
void ser() interrupt 4                                //串口中断程序
    {
        ES=0;                                        //关串口中断
        RI=0;                                        //清零串口接收标志位
        for ( i = 0; i < 13; i++)                    //在c语言中,数组是不能整体赋值的,即a[13]=SBUF是错误的,必须是逐个元素赋值才行
                                                            //读取卡号
                    {                                             
                        a[i] = SBUF;                        
                        while(!RI);
                        RI=0;
                    }
                flag=1;
                ES=1;                                        //开串口中断

        flag=1;
        ES=1;                                        //开串口中断
    }   
2017-10-12 12:36
快速回复:ID卡读/写卡程序,读的部分很成功,写的部分不成功,应该如何编程?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020818 second(s), 11 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved