请各位大神看看这个程序哪里有问题 找半天就是不能正常工作
帮一位朋友问的,他的问题是我是剛入門單晶片不久的渣新,
很多語法都不太懂,這問題困擾了我好幾天 ,翻書也翻了很久都沒找到答案。真的腦子已經快崩潰 ,所以前來跪求各位科技大大幫忙><請科技大大可以幫我看看我哪裡有錯。
我想將blinky和uart0結合, 利用uart在終端機控制 閃燈速度 但我才剛開始寫就卡關了。。。。。
但 主程式跳不到uart0的interrupt 只在裡面一直開關開關。我有兩個中斷 一個是控制閃燈 一個是控制uart0的。
uart0的接收有成功, 但是接收到的資訊不對,(我已經檢查也算過很多次baudrate 我覺得我是算對的),所以到要傳送的時候他條件不合不能傳送。目前苦於找不到錯誤原因可以的話, 請告訴我有甚麼錯 或不好的寫法也可以跟我講(我知道我錯很多的樣子。。。。或者是有甚麼辦法可以測試或解決。 本帖最后由 shejian001 于 2014-11-13 07:48 编辑
对不起,渣手机,只能上传照片,不能上传附件。
我还是复制吧,如果是乱码,请整理一下看。
点击下载程序附件
//------------------------------------------------------------------------------------// Blinky.c//------------------------------------------------------------------------------------//------------------------------------------------------------------------------------// Includes//------------------------------------------------------------------------------------#include <c8051f020.h> // SFR declarations#include <UART.h>//------------------------------------------------------------------------------------// 16-bit SFR Definitions for 'F02x//------------------------------------------------------------------------------------//sfr16 TMR2 = 0xCC; // Timer2sfr16 DP = 0x82; // data pointersfr16 TMR3RL = 0x92; // Timer3 reload valuesfr16 TMR3 = 0x94; // Timer3 countersfr16 ADC0 = 0xbe; // ADC0 datasfr16 ADC0GT = 0xc4; // ADC0 greater than windowsfr16 ADC0LT = 0xc6; // ADC0 less than windowsfr16 RCAP2 = 0xca; // Timer2 capture/reloadsfr16 T2 = 0xcc; // Timer2sfr16 RCAP4 = 0xe4; // Timer4 capture/reloadsfr16 T4 = 0xf4; // Timer4sfr16 DAC0 = 0xd2; // DAC0 datasfr16 DAC1 = 0xd5; // DAC1 data//------------------------------------------------------------------------------------// Global CONSTANTS//------------------------------------------------------------------------------------#define SYSCLK 16000000 // approximate SYSCLK frequency in Hz#define ACTIVE 1#define DEACTIVE 0#define MACC 0#define MALC 1#define BAUDRATE 9600 // Baud rate of UART0 in bps#define SYSTEMCLOCK 16000000sbit LED = P3^3; // green LED: '1' = ON; '0' = OFF//------------------------------------------------------------------------------------// Function PROTOTYPES//------------------------------------------------------------------------------------void PORT_1_Init (void);void Timer3_Init (int counts);void Timer3_ISR (void);void OSCILLATOR_Init (void);void UART0_Init (void);//------------------------------------------------------------------------------------// MAIN Routine//------------------------------------------------------------------------------------void main (void) {// disable watchdog timerWDTCN = 0xde;WDTCN = 0xad;OSCILLATOR_Init (); // Initialize oscillatorPORT_1_Init ();Timer3_Init (SYSCLK / 12 / 10); // InitTimer3 to generate interrupts// UART0_Init (); // Initialize UART0EA = 1; // enable global interruptswhile (1) {// If the complete word has been entered via the hyperterminal followed by// carriage returnif((TX_Ready == 1) && (UART_Buffer_Size != 0) && (Byte == 13)){TX_Ready = 0; // Set the flag to zeroSCON0 = (SCON0 | 0x02); // Set transmit flag to 1} // spin forever}}//-----------------------------------------------------------------------------// OSCILLATOR_Init//-----------------------------------------------------------------------------//// Return Value : None// Parameters : None//// This function initializes the system clock to use the external 22.1184MHz// crystal.////-----------------------------------------------------------------------------void OSCILLATOR_Init (void){OSCICN = 0x07; //16MHZ}//-----------------------------------------------------------------------------// PORT_1_Init//-----------------------------------------------------------------------------//// Return Value : None// Parameters : None//// This function configures the crossbar and GPIO ports.//// P0.0 digital push-pull UART TX// P0.1 digital open-drain UART RX//-----------------------------------------------------------------------------void PORT_1_Init(void){// char *mode;XBR0 = 0x07;// XBR1 = 0x14;XBR2 = 0x40;P1MDIN = 0x00;P0MDOUT = 0xFF;// P1MDOUT = 0x37;P2MDOUT = 0xFF;P3MDOUT = 0xFC;}//------------------------------------------------------------------------------------// Timer3_Init//------------------------------------------------------------------------------------//// Configure Timer3 to auto-reload and generate an interrupt at interval// specified by <counts> using SYSCLK/12 as its time base.//void Timer3_Init (int counts){TMR3CN = 0x00; // Stop Timer3; Clear TF3;// use SYSCLK/12 as timebaseTMR3RL = -counts; // Init reload valuesTMR3 = 0xffff; // set to reload immediatelyEIE2 |= 0x01; // enable Timer3 interruptsTMR3CN |= 0x04; // start Timer3}void UART0_Init (void){SCON0 = 0x50; // SCON1: mode 1, 8-bit UART, enable RXTMOD &= ~0xF0; // 0TMOD |= 0x20; // TMOD: timer 1, mode 2, 8-bit reloadif ( (((SYSTEMCLOCK/BAUDRATE)/256)/16) < 1 ){PCON |= 0x80; // SMOD1 (PCON.4)= 1 --> UART1 baudrate// divide-by-two disabledCKCON |= 0x10; // Timer1 uses the SYSTEMCLOCKTH1 = - ((SYSTEMCLOCK/BAUDRATE)/16/2);}else if ( (((SYSTEMCLOCK/BAUDRATE)/256)/32) < 1 ){PCON &= ~0x80; // SMOD1 (PCON.4) = 0 --> UART1 baudrate// divide-by-two enabledCKCON |= 0x10; // Timer1 uses the SYSTEMCLOCKTH1 = - ((SYSTEMCLOCK/BAUDRATE)/32/2);}else if ( ((((SYSTEMCLOCK/BAUDRATE)/256)/16)/12) < 1 ){PCON |= 0x80; // SMOD1 (PCON.4)= 1 --> UART1 baudrate// divide-by-two disabledCKCON &= ~0x10; // Timer1 uses the SYSTEMCLOCK/12TH1 = - (((SYSTEMCLOCK/BAUDRATE)/16)/12/2);}else if ( ((((SYSTEMCLOCK/BAUDRATE)/256)/32)/12) < 1 ){PCON &= ~0x80; // SMOD1 (PCON.4) = 0 --> UART1 baudrate// divide-by-two enabledCKCON &= ~0x10; // Timer1 uses the SYSTEMCLOCK/12TH1 = - (((SYSTEMCLOCK/BAUDRATE)/32)/12/2);}TL1 = TH1; // init Timer1TR1 = 1; // START Timer1TX_Ready = 1; // Flag showing that UART can transmitES0 = 1; // Enable UART0 interruptsIP =0x10;}//------------------------------------------------------------------------------------// Interrupt Service Routines//------------------------------------------------------------------------------------//------------------------------------------------------------------------------------// Timer3_ISR//------------------------------------------------------------------------------------// This routine changes the state of the LED whenever Timer3 overflows.//void Timer3_ISR (void) interrupt 14{TMR3CN &= ~(0x80); // clear TF3LED = ~LED; // change state of LED}//-----------------------------------------------------------------------------// UART1_Interrupt//-----------------------------------------------------------------------------//// This routine is invoked whenever a character is entered or displayed on the// Hyperterminal.////-----------------------------------------------------------------------------void UART0_Interrupt (void) interrupt 4{if ((SCON0 & 0x01) == 0x01){// Check if a new word is being enteredif( UART_Buffer_Size == 0) {UART_Input_First = 0; }SCON0 = (SCON0 & 0xFE); //RI1 = 0;Byte = SBUF0; // Read a character from Hyperterminalif (UART_Buffer_Size < UART_BUFFERSIZE){UART_Buffer = Byte; // Store characterUART_Buffer_Size++; // Update array's sizeUART_Input_First++; // Update counter}}if ((SCON0 & 0x02) == 0x02) // Checkif transmit flag is set{SCON0 = (SCON0 & 0xFD);if (UART_Buffer_Size != 1) // If buffernot empty{// Check if a new word is being outputif ( UART_Buffer_Size == UART_Input_First ){UART_Output_First = 0;}// Store a character in the variable byteByte = UART_Buffer;if ((Byte >= 0x61) && (Byte <= 0x7A)){ // If lower case letterByte -= 32; }SBUF0 = Byte;UART_Output_First++; // Update counterUART_Buffer_Size--; // Decrease array size}else{UART_Buffer_Size = 0; // Set the array size to 0TX_Ready = 1; // Indicate transmission complete}}}//-----------------------------------------------------------------------------// End Of File//------------------------------------------------------- 台湾兄弟吗{:shocked:} my二月兰 发表于 2014-11-13 07:59 台湾兄弟吗
不是湾就是港,不知道澳门是不是繁体字。 xinxinyu2013 发表于 2014-11-13 08:06 太乱了,没法看
下载附件看,手机复制的,没法排序。 台湾兄弟,你这神一样的代码排列没人看得懂啊 先一個一個來,先把UART搞定。
樓主沒有描述異常現象,不好確認問題點。 太乱啦, 看得眼睛都花啦 代码好乱。。。。 帮楼主一把。
//------------------------------------------------------------------------------------
// Blinky.c
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
// Includes
//------------------------------------------------------------------------------------
#include <c8051f020.h> // SFR declarations
#include <UART.h>
//------------------------------------------------------------------------------------
// 16-bit SFR Definitions for 'F02x
//------------------------------------------------------------------------------------
//sfr16 TMR2 = 0xCC; // Timer2
sfr16 DP = 0x82; // data pointer
sfr16 TMR3RL = 0x92; // Timer3 reload value
sfr16 TMR3 = 0x94; // Timer3 counter
sfr16 ADC0 = 0xbe; // ADC0 data
sfr16 ADC0GT = 0xc4; // ADC0 greater than window
sfr16 ADC0LT = 0xc6; // ADC0 less than window
sfr16 RCAP2 = 0xca; // Timer2 capture/reload
sfr16 T2 = 0xcc; // Timer2
sfr16 RCAP4 = 0xe4; // Timer4 capture/reload
sfr16 T4 = 0xf4; // Timer4
sfr16 DAC0 = 0xd2; // DAC0 data
sfr16 DAC1 = 0xd5; // DAC1 data
//------------------------------------------------------------------------------------
// Global CONSTANTS
//------------------------------------------------------------------------------------
#define SYSCLK 16000000 // approximate SYSCLK frequency in Hz
#define ACTIVE 1
#define DEACTIVE 0
#define MACC 0
#define MALC 1
#define BAUDRATE 9600 // Baud rate of UART0 in bps
#define SYSTEMCLOCK 16000000
sbitLED = P3^3; // green LED: '1' = ON; '0' = OFF
//------------------------------------------------------------------------------------
// Function PROTOTYPES
//------------------------------------------------------------------------------------
void PORT_1_Init (void);
void Timer3_Init (int counts);
void Timer3_ISR (void);
void OSCILLATOR_Init (void);
void UART0_Init (void);
//------------------------------------------------------------------------------------
// MAIN Routine
//------------------------------------------------------------------------------------
void main (void) {
// disable watchdog timer
WDTCN = 0xde;
WDTCN = 0xad;
OSCILLATOR_Init (); // Initialize oscillator
PORT_1_Init ();
Timer3_Init (SYSCLK / 12 / 10); // Init Timer3 to generate interrupts
// UART0_Init (); // Initialize UART0
EA = 1; // enable global interrupts
while (1) {
// If the complete word has been entered via the hyperterminal followed by
// carriage return
if((TX_Ready == 1) && (UART_Buffer_Size != 0) && (Byte == 13))
{
TX_Ready = 0; // Set the flag to zero
SCON0 = (SCON0 | 0x02); // Set transmit flag to 1
} // spin forever
}
}
//-----------------------------------------------------------------------------
// OSCILLATOR_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// This function initializes the system clock to use theexternal 22.1184MHz
// crystal.
//
//-----------------------------------------------------------------------------
void OSCILLATOR_Init (void)
{
OSCICN = 0x07; //16MHZ
}
//-----------------------------------------------------------------------------
// PORT_1_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// This function configures the crossbar and GPIO ports.
//
// P0.0 digital push-pull UART TX
// P0.1 digital open-drain UART RX
//-----------------------------------------------------------------------------
void PORT_1_Init(void)
{
// char *mode;
XBR0 = 0x07;
// XBR1 = 0x14;
XBR2 = 0x40;
P1MDIN = 0x00;
P0MDOUT = 0xFF;
// P1MDOUT = 0x37;
P2MDOUT = 0xFF;
P3MDOUT = 0xFC;
}
//------------------------------------------------------------------------------------
// Timer3_Init
//------------------------------------------------------------------------------------
//
// Configure Timer3 to auto-reload and generate an interrupt at interval
// specified by <counts> using SYSCLK/12 as its time base.
//
void Timer3_Init (int counts)
{
TMR3CN = 0x00; // Stop Timer3; Clear TF3;
// use SYSCLK/12 as timebase
TMR3RL= -counts; // Init reload values
TMR3 = 0xffff; // set to reload immediately
EIE2 |= 0x01; // enable Timer3 interrupts
TMR3CN |= 0x04; // start Timer3
}
void UART0_Init (void)
{
SCON0 = 0x50; // SCON1: mode 1, 8-bit UART, enable RX
TMOD &= ~0xF0; // 0
TMOD |=0x20; // TMOD: timer 1, mode 2, 8-bit reload
if ( (((SYSTEMCLOCK/BAUDRATE)/256)/16) < 1 )
{
PCON |= 0x80; // SMOD1 (PCON.4) = 1 --> UART1 baudrate
// divide-by-two disabled
CKCON |= 0x10; // Timer1 uses the SYSTEMCLOCK
TH1 = - ((SYSTEMCLOCK/BAUDRATE)/16/2);
}
else if ( (((SYSTEMCLOCK/BAUDRATE)/256)/32) < 1 )
{
PCON &= ~0x80; // SMOD1 (PCON.4) = 0 --> UART1 baudrate
// divide-by-two enabled
CKCON |= 0x10; // Timer1 uses the SYSTEMCLOCK
TH1 = - ((SYSTEMCLOCK/BAUDRATE)/32/2);
}
else if ( ((((SYSTEMCLOCK/BAUDRATE)/256)/16)/12) < 1 )
{
PCON |= 0x80; // SMOD1 (PCON.4) = 1 --> UART1 baudrate
// divide-by-two disabled
CKCON &= ~0x10; // Timer1 uses the SYSTEMCLOCK/12
TH1 = - (((SYSTEMCLOCK/BAUDRATE)/16)/12/2);
}
else if ( ((((SYSTEMCLOCK/BAUDRATE)/256)/32)/12) < 1 )
{
PCON &= ~0x80; // SMOD1 (PCON.4) = 0 --> UART1 baudrate
// divide-by-two enabled
CKCON &= ~0x10; // Timer1 uses the SYSTEMCLOCK/12
TH1 = - (((SYSTEMCLOCK/BAUDRATE)/32)/12/2);
}
TL1 = TH1; // init Timer1
TR1 = 1; // START Timer1
TX_Ready = 1; // Flag showing that UART can transmit
ES0 = 1; // Enable UART0 interrupts
IP =0x10;
}
//------------------------------------------------------------------------------------
// Interrupt Service Routines
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
// Timer3_ISR
//------------------------------------------------------------------------------------
// This routine changes the state of the LED whenever Timer3 overflows.
//
void Timer3_ISR (void) interrupt 14
{
TMR3CN &= ~(0x80); // clear TF3
LED = ~LED; // change state of LED
}
//-----------------------------------------------------------------------------
// UART1_Interrupt
//-----------------------------------------------------------------------------
//
// This routine is invoked whenever a character is entered or displayed on the
// Hyperterminal.
//
//-----------------------------------------------------------------------------
void UART0_Interrupt (void) interrupt 4
{
if ((SCON0 & 0x01) == 0x01)
{
// Check if a new word is being entered
if( UART_Buffer_Size == 0){
UART_Input_First = 0; }
SCON0 = (SCON0 & 0xFE); //RI1 = 0;
Byte = SBUF0; // Read a character from Hyperterminal
if (UART_Buffer_Size < UART_BUFFERSIZE)
{
UART_Buffer = Byte;// Store character
UART_Buffer_Size++; // Update array's size
UART_Input_First++; // Update counter
}
}
if ((SCON0 & 0x02) == 0x02) // Check if transmit flag is set
{
SCON0 = (SCON0 & 0xFD);
if (UART_Buffer_Size != 1) // If buffer not empty
{
// Check if a new word is being output
if ( UART_Buffer_Size == UART_Input_First )
{
UART_Output_First = 0;
}
// Store a character in the variable byte
Byte = UART_Buffer;
if ((Byte >= 0x61) && (Byte <= 0x7A)) { // If lower case letter
Byte -= 32; }
SBUF0 = Byte;
UART_Output_First++; // Update counter
UART_Buffer_Size--; // Decrease array size
}
else
{
UART_Buffer_Size = 0; // Set the array size to 0
TX_Ready = 1; // Indicate transmission complete
}
}
}
//-----------------------------------------------------------------------------
// End Of File
//------------------------------------------------------- shejian001 发表于 2014-11-13 08:07
不是湾就是港,不知道澳门是不是繁体字。
看了好几年的繁体字,有点怀念{:sad:}
页:
[1]