久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 3388|回復: 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 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 91在线观看视频 | 成人免费视频在线观看 | 国产高清在线观看 | 成人精品啪啪欧美成 | 国产精品久久久久久久久久久免费看 | 中文字幕91 | 2018天天干天天操 | 亚洲高清在线播放 | 日韩精品在线观看免费 | 色综合视频 | 麻豆精品国产91久久久久久 | 黄片毛片免费观看 | 欧美久久精品一级黑人c片 91免费在线视频 | 成人在线播放 | 国产97色 | 国产精品视频导航 | 精品国产乱码久久久久久果冻传媒 | 精国产品一区二区三区四季综 | 国产精品毛片无码 | 色婷婷一区 | 97国产精品视频人人做人人爱 | 国产欧美在线 | 97在线观视频免费观看 | 日韩欧美精品在线 | 青青草社区 | 久久亚洲一区二区三区四区 | 成人二区 | 欧美片网站免费 | 国产99久久精品一区二区永久免费 | 一级午夜aaa免费看三区 | 操操操av | 中文字字幕一区二区三区四区五区 | 欧美一区二区三区在线免费观看 | 在线亚洲一区 | 国产精品毛片久久久久久久 | 婷婷精品| 黄色网一级片 | 正在播放一区二区 | 欧美国产日韩在线观看 | 亚洲成人精品一区 | 亚洲一区二区三区国产 |