标题:读取卡号时总显示输给的初值
取消只看楼主
h1357951
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2017-5-2
结帖率:100%
已结贴  问题点数:20 回复次数:2 
读取卡号时总显示输给的初值
程序如下:
#include <reg52.h>                                    //包含头文件
#include <stdio.h>                                    //包含头文件
#include <string.h>                                    //包含头文件
#define uchar unsigned char                            //宏定义
#define uint unsigned int                            //宏定义
#define LCD_data  P0                                 //数据口
sbit int0=P3^0;                                        //串口读入
sbit mc_out=P2^1;                                        //打点脉冲输出位置
sbit LCD_RS  =  P2^2;            //数据/地址寄存器选择输入
sbit LCD_RW  =  P2^3;            //液晶读/写控制
sbit LCD_EN  =  P2^4;            //液晶使能控制
sbit LCD_PSB =  P2^7;            //串/并方式控制
sbit LCD_RST  =  P2^6;              //    lcd复位端
sbit LCD_vout  =  P2^5;             //倍压输出选择端

uchar code dis0[] = {"    欢迎使用"};
uchar code dis1[] = {"焊缝识别打点系统"};
uchar code dis2[]={"焊缝类型: 气压焊"};
uchar code dis3[]={"焊缝类型: 电阻焊"};
uchar code dis4[]={"焊缝类型: 法铝焊"};
uchar code dis5[]={"焊缝类型: 异常"};
uchar code dis6[]={"钢轨编号: "};
char flag,hex;                                            
char a[14]="0102030405670";                            //字符数组a
char b[14]="0a0b0c0d0e0f0";
int i,j,len;
void init();                                        //初始化子程序的声明
void delay(uint z);

void lcd_init();                                    //lcd初始化子程序的声明
void lcd_pos(uchar X,uchar Y);                        //初始位置子程序的声明
void pout(uint x);                                    //输出脉冲子程序的声明
uchar HexToAsc(uchar aChar)    ;                            //十六进制转换成ASCII码格式子程序的声明
uchar asctohex(uchar ahex)    ;                        // ASCII码格式格式转换成十六进制子程序的声明
//uchar asc2hex(uchar *str);
uint num,x,bb;
char temp,aChar,ascii;

/*******************************************************************/
/*                                                                 */
/*  1毫秒延时函数                                                       */
/*                                                                 */
/*******************************************************************/
void delay(uint z)
    {
       uint x,y;
        for(x=z;x>0;x--)
            for(y=110;y>0;y--);   
    }        
/*******************************************************************/
/*                                                                 */
/*  50微秒延时函数                                                       */
/*                                                                 */
/*******************************************************************/
void delay_50us    (uchar t)
    {
       uchar j;
        for(;t>0;t--)
            for(j=19;j>0;j--);   
    }   

/*******************************************************************/
/*                                                                 */
/*检查LCD忙状态                                                    */
/*lcd_busy为1时,忙,等待。lcd-busy为0时,闲,可写指令与数据。      */
/*                                                                 */
/*******************************************************************/
bit lcd_busy()
     {                          
        bit result;
        LCD_RS = 0;
        LCD_RW = 1;
        LCD_EN = 1;
        delay_50us(1);
        result = (bit)(P0&0x80);
        LCD_EN = 0;
        return(result);
     }         
/*******************************************************************/
/*                                                                 */
/*写指令数据到LCD                                                  */
/*RS=L,RW=L,E=高脉冲,D0-D7=指令码。                             */
/*                                                                 */
/*******************************************************************/
void lcd_wcmd(uchar cmd)
    {                          
        while(lcd_busy());
        LCD_RS = 0;
        LCD_RW = 0;
        LCD_EN = 0;
           delay_50us(2);
        P0 = cmd;
        LCD_EN = 1;//下降沿写入
        delay_50us(8);
        LCD_EN = 0;
        delay_50us(2);
     }
/*******************************************************************/
/*                                                                 */
/*写显示数据到LCD                                                  */
/*RS=H,RW=L,E=高脉冲,D0-D7=数据。                               */
/*   //显示字符                                                    */
/*******************************************************************/
void lcd_wdat(uchar dat)
    {                          
        while(lcd_busy());
        LCD_RS = 1;
        LCD_RW = 0;
        LCD_EN = 0;
           delay_50us(1);
        P0 = dat;
        LCD_EN = 1;//下降沿写入
        delay_50us(10);
        LCD_EN = 0;
        delay_50us(2);
    }
 void main()
    {   
        init();                                          //调用串口初始化子函数
        lcd_init();                                    //调用lcd初始化子函数
        while(1)
            {   
                   if(flag==1)   
                    {
                        ES=0;                        //关串口中断
                        flag=0;                        //清除flag标志位
                        lcd_wcmd(0x01);              //清除LCD的显示内容
                        delay(5);
                           i=0;
                        lcd_pos(0,0);               //设置显示位置为第一行的第1个字符
                        while(a[i]!= '\0')
                            {
                                lcd_wdat(a[i]);
                                i++    ;
                            }        
                /*            
                        switch (ascii)                 
                            {
                                case 2:                            //如果a[8]=0x32,执行下面操作
                                    num = 0;                    // num清零
                                    lcd_pos(0,0);                 //设置显示位置为第一行的第1个字符
                                    while(dis2[num] != '\0')
                                        {
                                              lcd_wdat(dis2[num]);      //显示字符 "焊缝类型:气压焊"
                                              num++;
                                        }
                                    pout(2);                      // 输出两个脉冲
                                    break;                        // 返回
                                            
                                case 3:                            //如果a[8]=0x33,执行下面操作
                                    num = 0;                    // num清零
                                    lcd_pos(0,0);                 //设置显示位置为第一行的第1个字符
                                    
                        
                                    while(dis3[num] != '\0')
                                        {
                                              lcd_wdat(dis3[num]);      //显示字符 "焊缝类型:电阻焊"
                                              num++;
                                        }
                                    pout(3);                      // 输出三个脉冲
                                    break;                        // 返回
                                            
                                case 4:                            //如果a[8]=0x34,执行下面操作
                                    num = 0;                    // num清零
                                    lcd_pos(0,0);                 //设置显示位置为第一行的第1个字符
                                    while(dis4[num] != '\0')
                                        {
                                              lcd_wdat(dis4[num]);      //显示字符 "焊缝类型:法铝焊"
                                              num++;
                                        }
                                    pout(4);                         // 输出四个脉冲
                                    break;                        //返回
                                default:                            //如果a[8]不等于上述各值,执行下面操作
                                    num = 0;                    // num清零
                                    lcd_pos(0,0);                 //设置显示位置为第一行的第1个字符
                                    while(dis5[num] != '\0')
                                        {
                                              lcd_wdat(dis5[num]);      //显示字符 "焊缝类型:异常"
                                              num++;
                                        }
                                    pout(1);                                 // 输出一个脉冲(异常)
                                    break;                        //返回
                            }            
                          */        
                             num=0;
                          lcd_pos(2,0);                 //设置显示位置为第三行的第1个字符
                        while(dis6[num] != '\0')
                            {
                                  lcd_wdat(dis6[num]);      //显示字符 "钢轨编号: "
                                  num++;
                            }                    
                         lcd_wdat(a[8]);
                        lcd_wdat(a[9]);
                        lcd_wdat(a[11]);
                          ES=1;                        //开串口中断
                     }     
            }
    }
                       
void ser() interrupt 4                                //串口中断程序
    {
        ES=0;                                        //关串口中断
        RI=0;                                             //清零串口接收标志位
        a[14]=SBUF;   
        flag=1;
        ES=1;                                        //开串口中断
    }                                            
void init()                                                                                                      
    {
             TMOD = 0x20;                                   // 定时器T1工作于8位自动重载模式, 用于产生波特率
        TH1 = 0xfd;                                 // 波特率9600
        TL1 = 0xfd;                                    // 波特率9600
        TR1 = 1;                                     // 启动定时器T1
        SCON = 0x50;                                // 设定串行口工作方式1,(SM0=0;SM1=1)及REN=1
        EA=1;                                        //开总中断
        ES=1;                                        //开串口中断
        mc_out=1;                                    //打点脉冲输出高电平
    }      
/*********************************************************/
/*                                                       */
/* 设定显示位置                                          */
/*                                                       */
/*********************************************************/
void lcd_pos(uchar X,uchar Y)                //显示地址
    {                          
       uchar  pos;
       if (X==0)
         {X=0x80;}
       else if (X==1)
         {X=0x90;}
       else if (X==2)
         {X=0x88;}
       else if (X==3)
         {X=0x98;}
       pos = X+Y ;  
       lcd_wcmd(pos);     //显示地址
    }

/*******************************************************************/
/*                                                                 */
/*  LCD初始化设定                                                  */
/*                                                                 */
/*******************************************************************/
void lcd_init()
    {
        LCD_PSB = 1;         //并口方式
        LCD_EN=0;
        LCD_RST=0;             //复位
        delay(5);
        LCD_RST=1;             //复位恢复
        lcd_wcmd(0x34);      //扩充指令操作
        delay(5);
        lcd_wcmd(0x30);      //基本指令操作
        delay(5);
        lcd_wcmd(0x0C);      //显示开,关光标
        delay(5);
        lcd_wcmd(0x01);      //清除LCD的显示内容
        delay(5);
        num = 0;               //欢迎界面开始
        lcd_wcmd(0x01);              //清除LCD的显示内容
        delay(5);
        lcd_pos(0,0);                 //设置显示位置为第一行的第1个字符
        while(dis0[num] != '\0')
            {
                  lcd_wdat(dis0[num]);      //显示字符 "欢迎使用"
                  num++;
            }
        num = 0;                        // num清零
        lcd_pos(2,0);                 //设置显示位置为第三行的第1个字符
        while(dis1[num] != '\0')
            {
                  lcd_wdat(dis1[num]);      //显示字符  "焊缝识别打点系统"
                  num++;
            }
        
               
    }
搜索更多相关主题的帖子: include 
2017-05-02 14:42
h1357951
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2017-5-2
得分:0 
代码中中断函数内的“a[14]=SBUF; ”一条语句即将卡号放入数组中,问题是:读出的不是该卡的卡号,而是定义数组(卡号)时设置的初值,如例中的a[14]="0102030405670" ,好像“a[14]=SBUF; ”一条语句根本不起作用,找不出问题所在。
           好友请留下联系方式:
           本人QQ:1186753690
2017-05-02 19:59
h1357951
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2017-5-2
得分:0 

将 a[14] = SBUF; 改为 for (int i = 0; i < 14; i++)
                          {
                                a[i]=SBUF;
                          }
系统提示:测试程序.C(205): error C141: syntax error near 'int'
          测试程序.C(205): error C141: syntax error near ')'
          Target not created
散去(int)后,恢复正常,发卡后只显示(卡号)第9位(a[9])的值,14位全是.
            i就是全局变量,找不出问题。
2017-05-03 11:16



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-476627-1-1.html




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

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