MQF2012916 发表于 2012-4-27 13:52:14

STC12C5A60S2单片机第二串口问题

用STCSTC12C5A60S2单片机收据接受没有问题?但单片机发送的数据却接收不到,小弟改了很久也不知道原因在哪儿?求各位高手帮忙看看哪儿出了问题?小弟不甚感激。。。下面是代码
#include<stc12c5as2.h>

#define uchar unsigned char

unsigned char code table[ ]={0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90};//controlshape
unsigned char list[ ]={0x09, 0x80, 0x07, 0x01, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00};
unsigned char ack[ ]={0x01, 0x80};

void initUart1()//9600bps@11.0592MHz       
{
        PCON &= 0x7f;                //波特率不倍速
        SCON = 0x50;                //8位数据,可变波特率
        BRT = 0xDC;                //设定独立波特率发生器重装值
        AUXR |= 0x04;                //独立波特率发生器时钟为Fosc,即1T
        AUXR |= 0x01;                //串口1选择独立波特率发生器为波特率发生器
        AUXR |= 0x10;                //启动独立波特率发生器
}

void initUart2(void)                //9600bps@11.0592MHz
{
        AUXR &= 0xf7;                //波特率不倍速
        S2CON = 0x50;                //8位数据,可变波特率
        BRT = 0xDC;                //设定独立波特率发生器重装值
        AUXR |= 0x04;                //独立波特率发生器时钟为Fosc,即1T
        AUXR |= 0x10;                //启动独立波特率发生器
}

void delay(void)      //延时函数,延时约0.6ms
{
        unsigned char i, j;
        for(j=0;j<10;j++)
      for(i=0;i<200;i++);
}

void display(unsigned char i)
{
P0 = table;
P2 = P2 & 0xf7; //11110111
delay();
P2 |= 0x01;
P2 = P2 & 0xfb; //11111011
delay();
P2 |= 0x02;
P2 = P2 & 0xfd; //11111101
delay();
P2 |= 0x04;
P2 = P2 & 0xfe; //11111110
delay();
P2 |= 0x08;
}

void ledOn(unsigned char i, unsigned char on)
{
if(on)
{
    P2 = P2 & (0xef<<i); //11101111
}
else
{
    P2 = P2 | (0x10<<i); //00010000
}
}

void SwitchOn(unsigned char on)
{
if(on)
{
    P3 &= 0xbf;//P4 &= 0xef;
}                           //10111111
else
{
    P3 |= 0x40;//P4 |= 0x10;
}                           //01000000
}

void ackList(uchar len)
{
uchar i;

    while(!(S2CON & 0x01)); //接收数据
    S2CON &= 0xfe;//11111110
    SBUF = S2BUF;

for(i = 0; i< 10; i++)
{
    S2BUF = list;
        //SBUF = list;
        while(!(S2CON & 0x02)); //发送数据
        S2CON &= 0xfd;
}
}

void ackWrite(uchar len)
{
uchar i;
while(!(S2CON & 0x01));
S2CON &= 0xfe;
SBUF = S2BUF;
while(!(S2CON & 0x01));
S2CON &= 0xfe;
list = S2BUF;
for(i = 0; i< len - 3; i++)
{
    while(!(S2CON & 0x01));
    S2CON &= 0xfe;
    SBUF = S2BUF;
}

ledOn(1, list);

for(i = 0; i< 2; i++)
{
    S2BUF = ack;
        //SBUF = list;
        while(!(S2CON & 0x02));
        S2CON &= 0xfd;
}
}

void main()
{
// unsigned char i= 0;
// unsigned char j = 0;
uchar len = 0;
uchar type = 0;

initUart1();
initUart2();
// P4SW |= 0x10;
display(0);

while(1)
{
    if(S2CON & 0x01)
        {
          S2CON &= 0xfe;
          len = S2BUF;
      SBUF = len;

          while(!(S2CON & 0x01));
          S2CON &= 0xfe;
          type = S2BUF;
          SBUF = type;
          if(type == 1)
          {
          ackList(len);
          }
          else if(type == 3)
          {
             ackWrite(len);
          }
          //while(1);
        }
        /*if(RI)
        {
          RI = 0;
      S2BUF = SBUF;
        }*/

        /*display(j);
        i++;
        if(i == 0)
        {
          ledOn(j/2, j%2);
          j = (j >= 9) ? 0 : (j + 1);
          if(j == 2)
          {
          SwitchOn(1);
          }
          if(j == 4)
          {
          SwitchOn(0);
          }
    }*/
}
}

MQF2012916 发表于 2012-4-27 13:59:43

来为高人求指点啊!{:handshake:}
页: [1]
查看完整版本: STC12C5A60S2单片机第二串口问题