📄 initmcu.c
字号:
//------------------------------------------------------------------------------
void InitUart1(void)
{
// Setting UART1 transmit/receive mode register (UART mode)
u1mr = 0x05; // XXXX XXXX
// |||| |||+- uart mode
// |||| ||+-- uart mode
// |||| |+--- uart mode
// |||| | 100: 7 bit data
// |||| | 101: 8 bit data
// |||| | 110: 9 bit data
// |||| +---- Internal/external clock select bit
// |||| 0: Internal clock
// |||| 1: External clock
// |||+------ Stop bit length select bit
// ||| 0: One stop bit
// ||| 1: Two stop bit
// ||+------- Odd/even parity select bit
// || Valid when bit 6 = 1
// || 0: Odd parity
// || 1: Even parity
// |+-------- Parity enable bit
// | 0: Parity disabled
// | 1: Parity enabled
// +--------- Sleep select bit
// 0: Sleep mode deselected
// 1: Sleep mode selected
// Setting UART1 transmit/receive control register 0 (UART mode)
u1c0 = 0x18; // 00XX XXXX
// |||| |||+- BRG count source select bit
// |||| ||+-- BRG count source select bit
// |||| || 00: f1 is selected
// |||| || 01: f8 is selected
// |||| || 10: f32 is selected
// |||| || 11: inhibited
// |||| |+--- /CTS//RTS function select bit
// |||| | (Valid when bit 4 ='0')
// |||| | 0: /CTS function is selected
// |||| | 1: /RTS function is selected
// |||| +---- Transmit register empty flag
// |||| 0: Data present in transmit register
// |||| (during transmission)
// |||| 1: No Data present in transmit register
// |||| (transmission completed)
// |||+------ /CTS//RTS disable bit
// ||| 0: /CTS//RTS function enabled
// ||| 1: /CTS//RTS function disabled
// ||+------- Data output select bit
// || 0: TxD0 pin is CMOS output
// || 1: TxD0 pin is N-channel open-drain
// || output
// |+-------- Must be fixed to '0'
// +--------- Must be fixed to '0'
// Setting UART1 transmit/receive control register 1 (UART mode)
u1c1 = 0x0f; // ---- X1X1
// |||+- Transmit enable bit
// ||| 0: Transmission disabled
// ||| 1: Transmission enabled
// ||+-- Transmit buffer empty flag
// || 0: Data present in transmit buffer register
// || 1: No data present in transmit buffer register
// |+--- Receive enable bit
// | 0: Reception disabled
// | 1: Reception enabled
// +---- Receive complete flag
// 0: No data present in receive buffer
// 1: Data present in receive buffer
// Setting UART1 transmit/receive control register 2 (UART mode)
ucon |= 0x00; // -X0- --XX
// || |+- UART0 transmit interrupt cause select bit
// || +- UART1 transmit interrupt cause select bit
// || 0: Transmit buffer empty (TI=1)
// || 1: Transmission completed (TXEPT=1)
// |+------- Must be fixed to '0'
// +-------- Separate /CTS//RTS bit
// 0: /CTS//RTS shared pin
// 1: /CTS//RTS separate pin
// Setting UART1 bit rate generator for 9600 baud (UART mode)
//u1brg = 0x40; // REMARKS: U1BRG=(Xin/(16*clock_select*Baud))-1
// For example:
// Xin = 10MHz
// clock_select = 1 (source=f1)
// Baud = 9600 Baud rate
// => u1brg = 64d = 0x40 (actual baud = 9615)
u1brg= 0x0A; //57600
s1ric = 0x04;
s1tic = 0x04;
handuart.head = 0;
handuart.tail = 0;
}
//------------------------------------------------------------------------------
// uart 1 tx interrupt
//------------------------------------------------------------------------------
void Uart1TxInt(void)
{
// OS_EXIT_CRITICAL();
if((u1c1 & 0x02)==0x02)
{
u1c1 &= ~0x02;
if(handuart.sendhead!=handuart.sendtail)
{
u1tb = handuart.sendbuf[handuart.sendhead];
//u1c1 |= 0x01;
handuart.sendhead++;
if(handuart.sendhead>=MAXHANDSENDBUFFER)handuart.sendhead = 0;
}
else
{
handuart.sendstatus = _IDLE;
}
}
}
//------------------------------------------------------------------------------
// uart 1 rx interrupt
//------------------------------------------------------------------------------
void Uart1RxInt(void)
{
WORD wsdata;
uchar u1rbbyte;
static uchar prebyte = 0;
// OS_EXIT_CRITICAL();
if((u1c1 & 0x08)==0x08)
{
u1c1 &= ~0x08;
wsdata = u1rb;
if(wsdata&0x8000)
{
u1c1 &= (~0x04);
u1c1 |= 0x04;
return;
}
u1rbbyte = (uchar)(wsdata&0x00ff);
if(prebyte==0x0d&&u1rbbyte ==0x0a)
{
uart1count = 0;
Send_Taskmsg(HandsetTaskid,HANDUARTRECV,NULL);
}
if(uart1count>180)
{
uart1count = 0;
Send_Taskmsg(HandsetTaskid,HANDUARTRECV,NULL);
//handuart.head = 0;
//handuart.tail = 0;
}
handuart.recvbuf[handuart.tail] = u1rbbyte;
//u1tb = u1rbbyte;
prebyte = u1rbbyte;
handuart.tail++;
uart1count++;
if(handuart.tail>=MAXHANDRECVBUFFER)handuart.tail=0;
}
}
//------------------------------------------------------------------------------
// initialize uart 2
//------------------------------------------------------------------------------
void InitUart2(void)
{
// Setting UART2 transmit/receive mode register (UART mode)
u2mr = 0x05; // XXXX XXXX
// |||| |||+- uart mode
// |||| ||+-- uart mode
// |||| |+--- uart mode
// |||| | 100: 7 bit data
// |||| | 101: 8 bit data
// |||| | 110: 9 bit data
// |||| +---- Internal/external clock select bit
// |||| 0: Internal clock
// |||| 1: External clock
// |||+------ Stop bit length select bit
// ||| 0: One stop bit
// ||| 1: Two stop bit
// ||+------- Odd/even parity select bit
// || Valid when bit 6 = 1
// || 0: Odd parity
// || 1: Even parity
// |+-------- Parity enable bit
// | 0: Parity disabled
// | 1: Parity enabled
// +--------- TxD, RxD I/O polarity reverse bit
// 0: No reverse
// 1: Reverse
// Setting UART2 transmit/receive control register 0 (UART mode)
u2c0 = 0x18; // 00XX XXXX
// |||| |||+- BRG count source select bit
// |||| ||+-- BRG count source select bit
// |||| || 00: f1 is selected
// |||| || 01: f8 is selected
// |||| || 10: f32 is selected
// |||| || 11: inhibited
// |||| |+--- /CTS//RTS function select bit
// |||| | (Valid when bit 4 ='0')
// |||| | 0: /CTS function is selected
// |||| | 1: /RTS function is selected
// |||| +---- Transmit register empty flag
// |||| 0: Data present in transmit register
// |||| (during transmission)
// |||| 1: No Data present in transmit register
// |||| (transmission completed)
// |||+------ /CTS//RTS disable bit
// ||| 0: /CTS//RTS function enabled
// ||| 1: /CTS//RTS function disabled
// ||+------- Nothing is assigned.
// |+-------- Must be fixed to '0'
// +--------- Transfer format select bit
// 0 : LSB first
// 1 : MSB first
// Setting UART2 transmit/receive control register 1 (UART mode)
u2c1 = 0x0f; // XXXXX1X1
// ||||||||+- Transmit enable bit
// |||||||| 0: Transmission disabled
// |||||||| 1: Transmission enabled
// ||||||||+-- Transmit buffer empty flag
// ||||||| 0: Data present in transmit buffer register
// ||||||| 1: No data present in transmit buffer register
// ||||||+--- Receive enable bit
// |||||| 0: Reception disabled
// |||||| 1: Reception enabled
// |||||+---- Receive complete flag
// |||| 0: No data present in receive buffer
// |||| 1: Data present in receive buffer
// |||+------ UART2 transmit interrupt cause select bit
// ||| 0 : Transmit buffer empty(TI = 1)
// ||| 1 : Transmit is completed(TXEPT = 1)
// ||+------- UART2 continuous receive mode enable bit,Must always be "0"
// |+-------- Data logic select bit
// | 0 : No reverse
// | 1 : Reverse
// +--------- Error signal output enable bit
// 0 : Output disabled
// 1 : Output enabled
u2smr=0; //UART2 special mode register
// Setting UART2 bit rate generator for 9600 baud (UART mode)
//u2brg = 0x40; // REMARKS: U1BRG=(Xin/(16*clock_select*Baud))-1
// For example:
// Xin = 10MHz
// clock_select = 1 (source=f1)
// Baud = 9600 Baud rate
// => u2brg = 64d = 0x40 (actual baud = 9615)
u2brg = 0x0a; //57600, clock_select = 1 (source=f1)
s2ric = 0x06;
s2tic = 0x05;
gsmuart.sendhead = 0;
gsmuart.sendtail = 0;
gsmuart.sendstatus = _IDLE;
gsmuart.head = 0;
gsmuart.tail = 0;
}
//------------------------------------------------------------------------------
// uart 2 recieve interrupt
//------------------------------------------------------------------------------
void Uart2RxInt(void)
{
uint wsdata;
uint u2rbbyte;
static uchar prebyte = 0;
// OS_EXIT_CRITICAL();
if((u2c1 & 0x08)==0x08)
{
u2c1 &= ~0x08;
wsdata = u2rb;
if(wsdata & 0x8000)
{
u2c1&=~0x04;
u2c1|=0x04;
return;
}
u2rbbyte = (uchar)(wsdata&0x00ff);
#ifdef DEBUG_AT
//u1tb = u2rbbyte;
#endif
if((prebyte==0x0d&&u2rbbyte==0x0a)||(u2rbbyte=='>')||(gsmstatus.gprsdata==TRUE&&u2rbbyte==0x03))
{
uart2count = 0;
Send_Taskmsg(GprsTaskid,GSMUARTRECV,NULL);
}
if(uart2count>600)
{
Send_Taskmsg(GprsTaskid,GSMUARTRECV,NULL);
uart2count = 0;
}
gsmuart.recvbuf[gsmuart.tail] = u2rbbyte;
prebyte = u2rbbyte;
gsmuart.tail++;
uart2count++;
if(gsmuart.tail>=MAXGSMRECVBUFFER)gsmuart.tail=0;
}
}
//------------------------------------------------------------------------------
// uart 2 send interrupt
//------------------------------------------------------------------------------
void Uart2TxInt(void)
{
// OS_EXIT_CRITICAL();
if((u2c1 & 0x02)==0x02)
{
u2c1 &= ~0x02;
if(gsmuart.sendhead!=gsmuart.sendtail)
{
#ifdef DEBUG_AT
//u1tb = gsmuart.sendbuf[gsmuart.sendhead];
#endif
u2tb = gsmuart.sendbuf[gsmuart.sendhead];
//u2c1 |= 0x01;
gsmuart.sendhead++;
if(gsmuart.sendhead>=MAXGSMSENDBUFFER){gsmuart.sendhead = 0;}
}
else
{
gsmuart.sendstatus = _IDLE;
}
}
}
//------------------------------------------------------------------------------
// initialize mcu
//------------------------------------------------------------------------------
void InitMcu(void)
{
OS_ENTER_CRITICAL(); //disable interupt
// prc1=1;
// pm02=0; //R/W mode select bit
// pm13=0;
// prc1=0;
cs0=1; //expand memory opened
cs0w=0; //Wait state inserted 0:Wait state inserted 1:no wait state
cs1=1;
cs1w=0;
cs2=1;
cs2w=0;
cs3=1;
cs3w=0; //by lrx
InitIO();
Init_Watchdog();
InitTa0();
StartTa0();
InitTa1();
StartTa1();
InitTa2(12);
StartTa2();
InitTa3(24);
InitInt0(); //added by leon 050929
InitInt1();
InitUart0();
InitUart1();
InitUart2();
OS_EXIT_CRITICAL(); //enable interupt
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -