|
我是剛入門單晶片不久的渣新,很多語法都不太懂,這問題困擾了我好幾天 翻書也翻了很久都沒找到答案 真的腦子已經快崩潰 所以前來跪求各位科技大大幫忙><
請科技大大可以幫我看看我哪裡有錯 我想將blinky和uart0結合 利用uart在終端機控制 閃燈速度 但我才剛開始寫就卡關了。。。。。 主程式跳不到uart0的interrupt 只在裡面一直開關開關
可以的話 請靠訴我有甚麼錯 或不好的寫法也可以跟我講(我知道我錯很多的樣子。。。。
//------------------------------------------------------------------------------------
// Blinky.c
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
// Includes
//------------------------------------------------------------------------------------
#include <c8051f020.h> // SFR declarations
#include <UART.h>
//------------------------------------------------------------------------------------
// 16-bit SFR Definitions for 'F02x
//------------------------------------------------------------------------------------
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 11059200L // approximate SYSCLK frequency in Hz
#define ACTIVE 1
#define DEACTIVE 0
#define MACC 0
#define MALC 1
#define BAUDRATE 9600 // Baud rate of UART in bps
#define SYSTEMCLOCK 11059200L
sbit 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 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 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[5];
XBR0 = 0x07;
// XBR1 = 0x14;
XBR2 = 0x40;
P1MDIN = 0x00;
P0MDOUT = 0xFF;
// P1MDOUT = 0x37;
P2MDOUT = 0xFF;
P3MDOUT = 0xFC;
AD5162_NSS = 1; //Potentiometer
M95128_NSS = 1; //EEPROM
AD5322_NSS = 1;
LDAC = 1;
#ifdef GUO
mode[1] = "D";
cmd_MODE(2, mode);
#else
/*
F_G_LlC = 1;
S_GlL_C = 1;
LD_SW = 1;
Mute_C = 1;
*/
// Mode Initial
LE=DEACTIVE;//0; //latch open
OE = DEACTIVE;//0;
F_G_LlC=MACC;//0;
S_GlL_C = ACTIVE;//1; //ACC
LD_SW = DEACTIVE;//0;
Mute_C = ACTIVE;//1;
//MODE D
// Mute_C = 0;
// S_GlL_C = 1;
OE = ACTIVE;//1; //LATCH OUTPUT OPEN
LE = ACTIVE;//1; //latch CLOSE
// AppRtDatas.RtFlag.Bits.OperationMode = 3;
// AppCfgMap.v.OperationMode = 3;
#endif
}
//------------------------------------------------------------------------------------
// 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;
TMOD |= 0x20; // TMOD: timer 1, mode 2, 8-bit reload
if ( (((SYSTEMCLOCK/BAUDRATE)/256)/16) < 1 )
{
PCON |= 0x10; // SMOD1 (PCON.4) = 1 --> UART1 baudrate
// divide-by-two disabled
CKCON |= 0x10; // Timer1 uses the SYSTEMCLOCK
TH1 = - ((SYSTEMCLOCK/BAUDRATE)/16);
}
TL1 = TH1; // init Timer1
TR1 = 1; // START Timer1
TX_Ready = 1; // Flag showing that UART can transmit
IE = 0x10; // Enable UART0 interrupts
IP = 0x10; // Make UART high priority
}
//------------------------------------------------------------------------------------
// 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[UART_Input_First] = 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[UART_Output_First];
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
//------------------------------------------------------- |
|