| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1924 人关注过本帖
标题:switch case语句的用法
只看楼主 加入收藏
h1357951
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2017-5-2
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:3 
switch case语句的用法
如下图:    switch (a[7])                 
                            {
                                case 2:                            //如果a[7]=2,执行下面操作
                                    lcd_pos(1,0);                 //设置显示位置为第二行的第1个字符
                                    while(dis2[num] != '\0')
                                        {
                                      pout(3);                      // 输出三个脉冲
                                    break;                            lcd_wdat(dis2[num]);      //显示字符 "焊缝类型:气压焊"
                                              num++;
                                        }
                                    pout(2);                      // 输出两个脉冲
                                    break;                        // 返回
                                case 3:                            //如果a[7]=3,执行下面操作
                                    lcd_pos(1,0);                 //设置显示位置为第二行的第1个字符
                                    while(dis3[num] != '\0')
                                        {
                                              lcd_wdat(dis3[num]);      //显示字符 "焊缝类型:电阻焊"
                                              num++;
编译时正常,但仿真时 case 2: ,case 3: 等语句总是不能被选中,不知问题何在,如何解决?
搜索更多相关主题的帖子: switch case 语句 显示 num 
2017-05-06 10:55
renkejun1942
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:不是这样
等 级:贵宾
威 望:33
帖 子:1645
专家分:5297
注 册:2016-12-1
收藏
得分:20 
要进入这些选项,取决于a[7]的值。
如果如你所说,那么你该确定a[7]的值到底是多少。
并且,a[7]中的值到底是数字,还是字符。

09:30 05/21 种下琵琶种子,能种活么?等待中……
21:50 05/27 没有发芽。
20:51 05/28 没有发芽。
23:03 05/29 没有发芽。
23:30 06/09 我有预感,要发芽了。
2017-05-06 11:47
h1357951
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2017-5-2
收藏
得分:0 
a[7]的值=2.
编译时正常,但仿真时,凡是 case 2: ,case 3: 的语句总是不能被选中,别的语句都正常。
2017-05-06 13:54
h1357951
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2017-5-2
收藏
得分:0 
重复一次,在下列程序中 switch case语句的用法表现为:编译时正常,但仿真时 case 2: ,case 3: 等语句总是不能被选中,不知问题何在,如何解决?就是说switch case语句不能正常运行,请帮解决。(用lcd12864屏显示).
#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];                            //字符数组a,a[14]为系统自动增设的终结符(\0)
char b[14];
int i,c;
void init();                                        //初始化子程序的声明
void delay(uint z);
void lcd_init();                                    //lcd初始化子程序的声明
void lcd_pos(uchar X,uchar Y);                        //初始位置子程序的声明
void pout(uint x);                                    //输出脉冲子程序的声明
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++    ;
                            }
            
                        c=a[7];
                        num = 0;                    // num清零            
                         switch (c)                 
                            {
                                case 2:                            //如果a[7]=2,执行下面操作
                                    lcd_pos(1,0);                 //设置显示位置为第二行的第1个字符
                                    while(dis2[num] != '\0')
                                        {
                                              lcd_wdat(dis2[num]);      //显示字符 "焊缝类型:气压焊"
                                              num++;
                                        }
                                    pout(2);                      // 输出两个脉冲
                                    break;                        // 返回
                                case 3:                            //如果a[7]=3,执行下面操作
                                    lcd_pos(1,0);                 //设置显示位置为第二行的第1个字符
                                    while(dis3[num] != '\0')
                                        {
                                              lcd_wdat(dis3[num]);      //显示字符 "焊缝类型:电阻焊"
                                              num++;
                                        }
                                    pout(3);                      // 输出三个脉冲
                                    break;                        // 返回
                                case 4:                            //如果a[7]=4,执行下面操作
                                    lcd_pos(1,0);                 //设置显示位置为第二行的第1个字符
                                    while(dis4[num] != '\0')
                                        {
                                              lcd_wdat(dis4[num]);      //显示字符 "焊缝类型:法铝焊"
                                              num++;
                                        }
                                    pout(4);                         // 输出四个脉冲
                                    break;                        //返回
                                default:                            //如果a[7]不等于上述各值,执行下面操作
                                    num = 0;                    // num清零
                                    lcd_pos(1,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[10]);
                     
                           ES=1;                        //开串口中断
                     }     
            }
    }
                       
void ser() interrupt 4                                //串口中断程序
    {
        ES=0;                                        //关串口中断
        RI=0;                                             //清零串口接收标志位
        for ( i = 0; i < 12; i++)                    //(将i<14改为i<12)去掉右边两位(终结符和结束位),这样更稳定 。
                                                    //       不能去掉奇数位,因为一字节占用两位。使用时不用左边三位
                                                    //       (一位起始位a[0]和两位卡的厂商标志位a[1],a[2])和右边一位a[11](校验和)。
                                                    //       剩余的八位(a[3]---a[10])十陆进制数已足够使用。
            {
                a[i] = SBUF;
                while(!RI);
                RI=0;
            }
        b[14]=a[14];
        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++;
            }
        
               
    }

 void pout(uint x)                    //根据焊缝类型 输出x个脉冲信号
    {
        for(; x >0; x--)
            {
                mc_out= 0;
                delay(500);
                mc_out= 1;
                delay(1000);
            }
    }
2017-05-07 09:20
快速回复:switch case语句的用法
数据加载中...
 
   



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

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