找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2868|回复: 15
打印 上一主题 下一主题
收起左侧

请高手指教串口问题,谢谢。

[复制链接]
跳转到指定楼层
楼主
ID:119239 发表于 2016-5-14 12:26 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
       SC12C5A60S2单片机,KEIL 4运行串口2程序,无法进入中断,串口1则可正常进入中断,不知何解。程序是正确的(是范例),包括从网上下载的多个串口2程序一样是无法进入中断服务程序,已困扰多日,请高手指点,谢谢。
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:120455 发表于 2016-5-14 21:15 | 只看该作者
程序发上来看看

评分

参与人数 1黑币 +20 收起 理由
admin + 20 回帖助人的奖励!

查看全部评分

回复

使用道具 举报

板凳
ID:119239 发表于 2016-5-15 09:59 | 只看该作者
烦请帮忙测试,谢谢。

我的串口2程序调不通(用STC12C5A60S2),怀疑是我的Keil  C,版本为UV4设置可能有问题。测试STC-ISP串口范例程序,串口1范例可正常执行,输出了字符串,串口2范例没有字符串输出,进入了死循环,不知何解?请朋友帮忙测试一下,究竟是何原因。

程序一:串口1例程(从STC-ISP烧写软件的范例中复制)
/*------------------------------------------------------------------*/
/* --- STC MCU Limited ---------------------------------------------*/
/* --- STC12C5Axx Series MCU UART (8-bit/9-bit)Demo ----------------*/
/* --- Mobile: (86)13922805190 -------------------------------------*/
/* --- Fax: 86-0513-55012956,55012947,55012969 ---------------------*/
/* --- Tel: 86-0513-55012928,55012929,55012966----------------------*/
/* If you want to use the program or the program referenced in the  */
/* article, please specify in which data and procedures from STC    */
/*------------------------------------------------------------------*/
#include "reg51.h"
#include "intrins.h"
typedef unsigned char BYTE;
typedef unsigned int WORD;
#define FOSC 11059200L      //System frequency
#define BAUD 9600           //UART baudrate
/*Define UART parity mode*/
#define NONE_PARITY     0   //None parity
#define ODD_PARITY      1   //Odd parity
#define EVEN_PARITY     2   //Even parity
#define MARK_PARITY     3   //Mark parity
#define SPACE_PARITY    4   //Space parity
#define PARITYBIT EVEN_PARITY   //Testing even parity
sbit bit9 = P2^2;           //P2.2 show UART data bit9
bit busy;
void SendData(BYTE dat);
void SendString(char *s);
void main()
{
#if (PARITYBIT == NONE_PARITY)
     SCON = 0x50;            //8-bit variable UART
#elif (PARITYBIT == ODD_PARITY) || (PARITYBIT == EVEN_PARITY) || (PARITYBIT == MARK_PARITY)
     SCON = 0xda;            //9-bit variable UART, parity bit initial to 1
#elif (PARITYBIT == SPACE_PARITY)
     SCON = 0xd2;            //9-bit variable UART, parity bit initial to 0
#endif
     TMOD = 0x20;            //Set Timer1 as 8-bit auto reload mode
     TH1 = TL1 = -(FOSC/12/32/BAUD); //Set auto-reload vaule
     TR1 = 1;                //Timer1 start run
     ES = 1;                 //Enable UART interrupt
     EA = 1;                 //Open master interrupt switch
     SendString("STC12C5A60S2\r\nUart Test !\r\n");
     while(1);
}
/*----------------------------
UART interrupt service routine
----------------------------*/
void Uart_Isr() interrupt 4 using 1
{
     if (RI)
     {
         RI = 0;             //Clear receive interrupt flag
         P0 = SBUF;          //P0 show UART data
         bit9 = RB8;         //P2.2 show parity bit
     }
     if (TI)
     {
         TI = 0;             //Clear transmit interrupt flag
         busy = 0;           //Clear transmit busy flag
     }
}
/*----------------------------
Send a byte data to UART
Input: dat (data to be sent)
Output:None
----------------------------*/
void SendData(BYTE dat)
{
     while (busy);           //Wait for the completion of the previous data is sent
     ACC = dat;              //Calculate the even parity bit P (PSW.0)
     if (P)                  //Set the parity bit according to P
     {
#if (PARITYBIT == ODD_PARITY)
         TB8 = 0;            //Set parity bit to 0
#elif (PARITYBIT == EVEN_PARITY)
         TB8 = 1;            //Set parity bit to 1
#endif
     }
     else
     {
#if (PARITYBIT == ODD_PARITY)
         TB8 = 1;            //Set parity bit to 1
#elif (PARITYBIT == EVEN_PARITY)
         TB8 = 0;            //Set parity bit to 0
#endif
     }
     busy = 1;
     SBUF = ACC;             //Send data to UART buffer
}
/*----------------------------
Send a string to UART
Input: s (address of string)
Output:None
----------------------------*/
void SendString(char *s)
{
     while (*s)              //Check the end of the string
     {
         SendData(*s++);     //Send current char and increment string ptr
     }
}



程序二:串口2例程(从STC-ISP烧写软件的范例中复制)
/*------------------------------------------------------------------*/
/* --- STC MCU Limited ---------------------------------------------*/
/* --- STC12C5Axx Series MCU UART2 (8-bit/9-bit)Demo ---------------*/
/* --- Mobile: (86)13922805190 -------------------------------------*/
/* --- Fax: 86-0513-55012956,55012947,55012969 ---------------------*/
/* --- Tel: 86-0513-55012928,55012929,55012966----------------------*/
/* --- Web: www.STCMCU.com -----------------------------------------*/
/* --- Web: www.GXWMCU.com -----------------------------------------*/
/* If you want to use the program or the program referenced in the  */
/* article, please specify in which data and procedures from STC    */
/*------------------------------------------------------------------*/
#include "reg51.h"
#include "intrins.h"
typedef unsigned char BYTE;
typedef unsigned int WORD;
#define FOSC 11059200L      //System frequency
#define BAUD 115200         //UART baudrate
/*Define UART parity mode*/
#define NONE_PARITY     0   //None parity
#define ODD_PARITY      1   //Odd parity
#define EVEN_PARITY     2   //Even parity
#define MARK_PARITY     3   //Mark parity
#define SPACE_PARITY    4   //Space parity
#define PARITYBIT EVEN_PARITY   //Testing even parity
/*Declare SFR associated with the UART2 */
sfr AUXR  = 0x8e;           //Auxiliary register
sfr S2CON = 0x9a;           //UART2 control register
sfr S2BUF = 0x9b;           //UART2 data buffer
sfr BRT   = 0x9c;           //Baudrate generator
sfr IE2   = 0xaf;           //Interrupt control 2
#define S2RI  0x01          //S2CON.0
#define S2TI  0x02          //S2CON.1
#define S2RB8 0x04          //S2CON.2
#define S2TB8 0x08          //S2CON.3
bit busy;
void SendData(BYTE dat);
void SendString(char *s);
void main()
{
#if (PARITYBIT == NONE_PARITY)
     S2CON = 0x50;           //8-bit variable UART
#elif (PARITYBIT == ODD_PARITY) || (PARITYBIT == EVEN_PARITY) || (PARITYBIT == MARK_PARITY)
     S2CON = 0xda;           //9-bit variable UART, parity bit initial to 1
#elif (PARITYBIT == SPACE_PARITY)
     S2CON = 0xd2;           //9-bit variable UART, parity bit initial to 0
#endif
     BRT = -(FOSC/32/BAUD);  //Set auto-reload vaule of baudrate generator
     AUXR = 0x14;            //Baudrate generator work in 1T mode
     IE2 = 0x01;             //Enable UART2 interrupt
     EA = 1;                 //Open master interrupt switch
     SendString("STC12C5A60S2\r\nUart2 Test !\r\n");
     while(1);
}
/*----------------------------
UART2 interrupt service routine
----------------------------*/
void Uart2() interrupt 8 using 1
{
     if (S2CON & S2RI)
     {
         S2CON &= ~S2RI;     //Clear receive interrupt flag
         P0 = S2BUF;         //P0 show UART data
         P2 = (S2CON & S2RB8);//P2.2 show parity bit
     }
     if (S2CON & S2TI)
     {
         S2CON &= ~S2TI;     //Clear transmit interrupt flag
         busy = 0;           //Clear transmit busy flag
     }
}
/*----------------------------
Send a byte data to UART
Input: dat (data to be sent)
Output:None
----------------------------*/
void SendData(BYTE dat)
{
     while (busy);           //Wait for the completion of the previous data is sent
     ACC = dat;              //Calculate the even parity bit P (PSW.0)
     if (P)                  //Set the parity bit according to P
     {
#if (PARITYBIT == ODD_PARITY)
         S2CON &= ~S2TB8;    //Set parity bit to 0
#elif (PARITYBIT == EVEN_PARITY)
         S2CON |= S2TB8;     //Set parity bit to 1
#endif
     }
     else
     {
#if (PARITYBIT == ODD_PARITY)
         S2CON |= S2TB8;     //Set parity bit to 1
#elif (PARITYBIT == EVEN_PARITY)
         S2CON &= ~S2TB8;    //Set parity bit to 0
#endif
     }
     busy = 1;
     S2BUF = ACC;            //Send data to UART2 buffer
}
/*----------------------------
Send a string to UART
Input: s (address of string)
Output:None
----------------------------*/
void SendString(char *s)
{
     while (*s)              //Check the end of the string
     {
         SendData(*s++);     //Send current char and increment string ptr
     }
}


回复

使用道具 举报

地板
ID:119239 发表于 2016-5-15 13:13 | 只看该作者
程序一:串口1例程(从STC-ISP烧写软件的范例中复制)
/*------------------------------------------------------------------*/
/* --- STC MCU Limited ---------------------------------------------*/
/* --- STC12C5Axx Series MCU UART (8-bit/9-bit)Demo ----------------*/
/* --- Mobile: (86)13922805190 -------------------------------------*/
/* --- Fax: 86-0513-55012956,55012947,55012969 ---------------------*/
/* --- Tel: 86-0513-55012928,55012929,55012966----------------------*/
/* If you want to use the program or the program referenced in the  */
/* article, please specify in which data and procedures from STC    */
/*------------------------------------------------------------------*/
#include "reg51.h"
#include "intrins.h"
typedef unsigned char BYTE;
typedef unsigned int WORD;
#define FOSC 11059200L      //System frequency
#define BAUD 9600           //UART baudrate
/*Define UART parity mode*/
#define NONE_PARITY     0   //None parity
#define ODD_PARITY      1   //Odd parity
#define EVEN_PARITY     2   //Even parity
#define MARK_PARITY     3   //Mark parity
#define SPACE_PARITY    4   //Space parity
#define PARITYBIT EVEN_PARITY   //Testing even parity
sbit bit9 = P2^2;           //P2.2 show UART data bit9
bit busy;
void SendData(BYTE dat);
void SendString(char *s);
void main()
{
#if (PARITYBIT == NONE_PARITY)
     SCON = 0x50;            //8-bit variable UART
#elif (PARITYBIT == ODD_PARITY) || (PARITYBIT == EVEN_PARITY) || (PARITYBIT == MARK_PARITY)
     SCON = 0xda;            //9-bit variable UART, parity bit initial to 1
#elif (PARITYBIT == SPACE_PARITY)
     SCON = 0xd2;            //9-bit variable UART, parity bit initial to 0
#endif
     TMOD = 0x20;            //Set Timer1 as 8-bit auto reload mode
     TH1 = TL1 = -(FOSC/12/32/BAUD); //Set auto-reload vaule
     TR1 = 1;                //Timer1 start run
     ES = 1;                 //Enable UART interrupt
     EA = 1;                 //Open master interrupt switch
     SendString("STC12C5A60S2\r\nUart Test !\r\n");
     while(1);
}
/*----------------------------
UART interrupt service routine
----------------------------*/
void Uart_Isr() interrupt 4 using 1
{
     if (RI)
     {
         RI = 0;             //Clear receive interrupt flag
         P0 = SBUF;          //P0 show UART data
         bit9 = RB8;         //P2.2 show parity bit
     }
     if (TI)
     {
         TI = 0;             //Clear transmit interrupt flag
         busy = 0;           //Clear transmit busy flag
     }
}
/*----------------------------
Send a byte data to UART
Input: dat (data to be sent)
Output:None
----------------------------*/
void SendData(BYTE dat)
{
     while (busy);           //Wait for the completion of the previous data is sent
     ACC = dat;              //Calculate the even parity bit P (PSW.0)
     if (P)                  //Set the parity bit according to P
     {
#if (PARITYBIT == ODD_PARITY)
         TB8 = 0;            //Set parity bit to 0
#elif (PARITYBIT == EVEN_PARITY)
         TB8 = 1;            //Set parity bit to 1
#endif
     }
     else
     {
#if (PARITYBIT == ODD_PARITY)
         TB8 = 1;            //Set parity bit to 1
#elif (PARITYBIT == EVEN_PARITY)
         TB8 = 0;            //Set parity bit to 0
#endif
     }
     busy = 1;
     SBUF = ACC;             //Send data to UART buffer
}
/*----------------------------
Send a string to UART
Input: s (address of string)
Output:None
----------------------------*/
void SendString(char *s)
{
     while (*s)              //Check the end of the string
     {
         SendData(*s++);     //Send current char and increment string ptr
     }
}



程序二:串口2例程(从STC-ISP烧写软件的范例中复制)
/*------------------------------------------------------------------*/
/* --- STC MCU Limited ---------------------------------------------*/
/* --- STC12C5Axx Series MCU UART2 (8-bit/9-bit)Demo ---------------*/
/* --- Mobile: (86)13922805190 -------------------------------------*/
/* --- Fax: 86-0513-55012956,55012947,55012969 ---------------------*/
/* --- Tel: 86-0513-55012928,55012929,55012966----------------------*/
/* --- Web: www.STCMCU.com -----------------------------------------*/
/* --- Web: www.GXWMCU.com -----------------------------------------*/
/* If you want to use the program or the program referenced in the  */
/* article, please specify in which data and procedures from STC    */
/*------------------------------------------------------------------*/
#include "reg51.h"
#include "intrins.h"
typedef unsigned char BYTE;
typedef unsigned int WORD;
#define FOSC 11059200L      //System frequency
#define BAUD 115200         //UART baudrate
/*Define UART parity mode*/
#define NONE_PARITY     0   //None parity
#define ODD_PARITY      1   //Odd parity
#define EVEN_PARITY     2   //Even parity
#define MARK_PARITY     3   //Mark parity
#define SPACE_PARITY    4   //Space parity
#define PARITYBIT EVEN_PARITY   //Testing even parity
/*Declare SFR associated with the UART2 */
sfr AUXR  = 0x8e;           //Auxiliary register
sfr S2CON = 0x9a;           //UART2 control register
sfr S2BUF = 0x9b;           //UART2 data buffer
sfr BRT   = 0x9c;           //Baudrate generator
sfr IE2   = 0xaf;           //Interrupt control 2
#define S2RI  0x01          //S2CON.0
#define S2TI  0x02          //S2CON.1
#define S2RB8 0x04          //S2CON.2
#define S2TB8 0x08          //S2CON.3
bit busy;
void SendData(BYTE dat);
void SendString(char *s);
void main()
{
#if (PARITYBIT == NONE_PARITY)
     S2CON = 0x50;           //8-bit variable UART
#elif (PARITYBIT == ODD_PARITY) || (PARITYBIT == EVEN_PARITY) || (PARITYBIT == MARK_PARITY)
     S2CON = 0xda;           //9-bit variable UART, parity bit initial to 1
#elif (PARITYBIT == SPACE_PARITY)
     S2CON = 0xd2;           //9-bit variable UART, parity bit initial to 0
#endif
     BRT = -(FOSC/32/BAUD);  //Set auto-reload vaule of baudrate generator
     AUXR = 0x14;            //Baudrate generator work in 1T mode
     IE2 = 0x01;             //Enable UART2 interrupt
     EA = 1;                 //Open master interrupt switch
     SendString("STC12C5A60S2\r\nUart2 Test !\r\n");
     while(1);
}
/*----------------------------
UART2 interrupt service routine
----------------------------*/
void Uart2() interrupt 8 using 1
{
     if (S2CON & S2RI)
     {
         S2CON &= ~S2RI;     //Clear receive interrupt flag
         P0 = S2BUF;         //P0 show UART data
         P2 = (S2CON & S2RB8);//P2.2 show parity bit
     }
     if (S2CON & S2TI)
     {
         S2CON &= ~S2TI;     //Clear transmit interrupt flag
         busy = 0;           //Clear transmit busy flag
     }
}
/*----------------------------
Send a byte data to UART
Input: dat (data to be sent)
Output:None
----------------------------*/
void SendData(BYTE dat)
{
     while (busy);           //Wait for the completion of the previous data is sent
     ACC = dat;              //Calculate the even parity bit P (PSW.0)
     if (P)                  //Set the parity bit according to P
     {
#if (PARITYBIT == ODD_PARITY)
         S2CON &= ~S2TB8;    //Set parity bit to 0
#elif (PARITYBIT == EVEN_PARITY)
         S2CON |= S2TB8;     //Set parity bit to 1
#endif
     }
     else
     {
#if (PARITYBIT == ODD_PARITY)
         S2CON |= S2TB8;     //Set parity bit to 1
#elif (PARITYBIT == EVEN_PARITY)
         S2CON &= ~S2TB8;    //Set parity bit to 0
#endif
     }
     busy = 1;
     S2BUF = ACC;            //Send data to UART2 buffer
}
/*----------------------------
Send a string to UART
Input: s (address of string)
Output:None
----------------------------*/
void SendString(char *s)
{
     while (*s)              //Check the end of the string
     {
         SendData(*s++);     //Send current char and increment string ptr
     }
}

评分

参与人数 1黑币 +20 收起 理由
admin + 20 回帖助人的奖励!

查看全部评分

回复

使用道具 举报

5#
ID:120455 发表于 2016-5-15 15:44 | 只看该作者
你换SC12C5A60S2的头文件试试呢?

评分

参与人数 1黑币 +20 收起 理由
admin + 20 回帖助人的奖励!

查看全部评分

回复

使用道具 举报

6#
ID:119239 发表于 2016-5-15 21:55 | 只看该作者
已换STC12C5A60S2头文件还是一样串口2进入不了中断服务程序,清不了“busy”忙标志,程序进入while(busy);死循环,不知何解,能否帮忙测一下串口2程序能否正常运行,从串口2窗中观察到输出的字符串,谢谢。

评分

参与人数 1黑币 +20 收起 理由
admin + 20 回帖助人的奖励!

查看全部评分

回复

使用道具 举报

7#
ID:119239 发表于 2016-5-16 15:07 | 只看该作者
一周都无人解答,花1分钟帮忙测试都不愿意,真失望。
回复

使用道具 举报

8#
ID:121114 发表于 2016-5-16 16:44 | 只看该作者
耐心等待吧!会有大神出现的! 这个应该很难吧!
这论坛资料挺多的 我还考虑开会员  现在........

评分

参与人数 1黑币 +20 收起 理由
admin + 20 回帖助人的奖励!

查看全部评分

回复

使用道具 举报

9#
ID:119239 发表于 2016-5-16 17:02 | 只看该作者
亘黑鹿你好,能否帮忙测试一下程序2是否正常运行,输出字符串STC12C5A60S2\r\nUart Test !,谢谢。
回复

使用道具 举报

10#
ID:121114 发表于 2016-5-16 20:23 | 只看该作者
yyg123321a 发表于 2016-5-16 17:02
亘黑鹿你好,能否帮忙测试一下程序2是否正常运行,输出字符串STC12C5A60S2\r\nUart Test !,谢谢。

我帮测?太看得起我了! 我刚学不久的  你写哪些太高级呀! 要我咋帮你!
回复

使用道具 举报

11#
ID:121114 发表于 2016-5-16 20:26 | 只看该作者
yyg123321a 发表于 2016-5-16 17:02
亘黑鹿你好,能否帮忙测试一下程序2是否正常运行,输出字符串STC12C5A60S2\r\nUart Test !,谢谢。

我有keil软件呀! 你是想我帮你运行程序有没有错误提示???
回复

使用道具 举报

12#
ID:121114 发表于 2016-5-16 20:31 | 只看该作者
yyg123321a 发表于 2016-5-16 17:02
亘黑鹿你好,能否帮忙测试一下程序2是否正常运行,输出字符串STC12C5A60S2\r\nUart Test !,谢谢。

我用keil测了程序  吾错误无警告!
回复

使用道具 举报

13#
ID:113415 发表于 2016-5-16 21:14 | 只看该作者
本帖最后由 baofu 于 2016-5-16 21:35 编辑

我编译链接中没有发现问题,但在debug中确实发现你所说的情况。但这也不能证明程序就有问题,只有实际运行才能确定,所以建议你搭建实际电路验证。并且建议初学者不要在模拟调试上花费很多时间,因为有些东西即使模拟调试成功,到了真实环境中也不一定就好用,结果也仅仅是参考性的,特别像这种含有中断的程序,模拟调试与实际情况相去甚远。
       还有一种情况,STC单片机在Keilc的型号数据库中没有被收录,都是借用相近型号,然后在程序中自定义特殊功能寄存器。这就造成两者不相符合。我没有实际验证,仅仅是猜测,供你参考。
回复

使用道具 举报

14#
ID:119239 发表于 2016-5-16 22:36 | 只看该作者
谢谢两位朋友回复,我也是下载了多个串口2的程序,编译链接都无错误,调试时都出现死循环(程序应是正确的,不可能都有错吧)。折腾多天也没头绪,一般我是先调试好软件再弄硬件的,再次谢谢baofu,我弄个硬件验证。
回复

使用道具 举报

15#
ID:119239 发表于 2016-5-20 09:42 | 只看该作者
      问题已解决,在最少系统运行串口2程序,可以通过串口助手观察到输出的字符串,非常感谢baofu。
但是还有点遗憾,不能DEBUG了,第一次用STC单片机,感觉其兼容性不佳,不能很好适应KEIL。还有STC公司做事不严紧,其官方发布的PDF资料和STC-ISP头文件关于中断优先级有误,一个写IPH2,另一个写IP2H,致电STC公司,其客服人员回复查证后更改。这说明STC公司对其发行的软件根本未做测试,否则不可能发生如此低级错误,也没有任何提示性信息,对于一间技术性公司来说是很不负责任的。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表