|
#include "Include.h"
" }" x0 O; f8 @: L7 }! t& ^( T' n1 m4 a' Z* f- b& y1 F9 m, j6 j/ Q
unsigned char xdata uart_rx_buf[128];* i0 u. H: Y+ ^, [/ M) P, E
unsigned char xdata uart_tx_buf[128];
8 r, t- T$ {! T9 R; W; Lvolatile UART_T xdata uart;
/ ?, t* z; x0 ^: l7 aunsigned char xdata uartGetBuf[8];
# P3 L% W8 S! V- T2 Bunsigned char RxCmdPt,RxCmdTimeOut;
3 E4 I8 i' G6 Q
4 m, F6 C) w$ Wstatic void Serial0(void)interrupt 4 using 1( L) F4 r9 M0 B) P) i* e
{
* e6 C+ J3 K( @1 T0 x, I( k unsigned char temp,head;
% ]& }' k9 t% t: n/ o& P4 V% t% q) R1 ^# k6 J* f2 O* N* c
if(_testbit_(RI))
( o4 s; d/ m( D' r/ C/ c {
) [* I; y8 W y9 _: n( m6 U; X temp = SBUF;
: G" m( _9 h8 u7 i% E head = RBUF_NEXT_PT(uart.rx.head,sizeof(uart_rx_buf));
3 [" x4 U" V. S" z2 ~2 O if(head != uart.rx.tail)1 g1 F3 l/ w: H& k z3 k3 f# V
{* D. c& C, m1 i$ l, w) L
uart_rx_buf[uart.rx.head] = temp;2 j# O8 p/ Q6 ^2 z1 R: ^2 |( A% C5 o
uart.rx.head = head;# A% M$ F. q( z/ e
}else9 J- O3 `- A/ t' Y/ F' e' _2 }4 n
{0 v( g3 D7 n+ m2 x% z& a
uart.rx.error = 1;
) x- H9 q9 S! k. e }8 S( b, |! X) a* g, @
}
( I& F. n9 S1 m if(_testbit_(TI))& M2 I6 b l* G9 k l
{; A; H4 o' D% K8 u
if(uart.tx.head == uart.tx.tail)& c/ H0 V& b# o+ m! U
{
+ b3 M" I5 r. c uart.tx.busy = 0;
! Y0 U- l/ o2 u8 H+ S }else
. _$ ?4 P5 Z3 o/ x5 c {) ?, z3 h5 { G: H
SBUF = uart_tx_buf[uart.tx.tail];9 Z; ^: W! u8 q+ Q0 u8 d
uart.tx.tail = RBUF_NEXT_PT(uart.tx.tail,sizeof(uart_tx_buf));
( V3 u4 d, Y; w7 T/ V2 f- p }8 v& D; \$ ^- D* B; K/ @
}: V/ X W: U' x
}6 Q- ]8 M! `4 C, Y" ~7 D! ^" c, l
//函数 串口中断初始化
# Z( `% S/ |: v3 t ~) I q3 wextern void UartInit(void)
; R# m" u8 {# E4 ^& `( G1 [1 z{
9 B5 u$ m2 c. o RCAP2L = -(XTAL/32/BaudRate_9600);# M3 b6 I2 @- r
RCAP2H = (-(XTAL/32/BaudRate_9600))>>8;
) b9 }. S6 g/ B- o% O [' G, F; n5 Z3 z. T. Y9 |! \/ w
T2CON = 0x34;
4 L# L. p' z% K" Q SCON = 0x50;; b2 | }' y6 Z' h7 F+ D1 g* i- b
6 T5 ^* D7 y+ B' X
IP |= 0x10; //serial 0 high priority
9 e. X8 |$ s' C. k( D' x2 S8 ? IE |= 0x10; //enable serial 05 l" P2 \0 x }' ` [
( m; F$ r- K2 n
memset(&uart,0,sizeof(uart));
# @$ k3 s6 l5 q}, @! T) L, m6 K& ^
// put char; |+ Z' B0 f* u" m) q
extern char Putchar(uchar c)
) E- E+ U/ }0 W5 n9 A! ^3 q* Y{7 y1 o; T4 a, o5 @. T
unsigned char head;
I! \; P( _ o" \
" E" s8 ^- [# N$ }- f. g head = RBUF_NEXT_PT(uart.tx.head,sizeof(uart_tx_buf));
+ k. G* \8 T- S. v! `# F, N1 H uart_tx_buf[uart.tx.head] = c;
5 i# h k) U u uart.tx.head = head;
9 R8 x3 }. Q7 y0 H6 i4 M/ C" r/ `, r! C0 |" z
if(uart.tx.busy != 0x5a)
& \0 T* x' o4 q {
$ l6 e; W5 o& v" n; y9 Y2 z, q uart.tx.busy = 0x5a;, ~7 \ Q, E7 I1 m' t: k
TI = 1;2 k2 b3 r& v& Z% z; g5 l
}
. Z7 e8 Q% @' `8 j$ w! Q9 s return c;
' ?; c& {; |0 H9 V) T) u/ _}
5 w7 }3 Z- z- q+ Y8 J0 u3 M! I& X//函数 判断串口是否有数据传进来
/ Z3 X1 d) v, A8 l9 Y// 如果有,进行接收数据2 o B9 t7 B! z- t8 t3 n: j
extern uchar GetRxBufSize(void) }7 @( F/ j; Z. z$ r# E4 a
{
! j. L- L7 n1 `" a+ W$ @) [ unsigned char head;+ _: S! N: V( [; V& j
z! y q Z9 U+ ]. u
head = uart.rx.head;
9 A6 K$ V0 c, x% n) P% ` if(head < uart.rx.tail)
; a& j. U6 c1 F {
& W, x% d8 _$ l* e return (sizeof(uart_rx_buf) - (uart.rx.tail - head));
0 X4 U$ H2 y1 n, L }else
7 H) O0 a# e$ Q( P) B {3 e) s4 ^. W( Q. [6 Y- ]: {
return (head - uart.rx.tail);
# N8 O/ i" Q( O4 {8 V }
5 |+ y9 ?; [: b, b2 }5 _* r6 f}3 k4 o5 [: n8 C$ L9 X0 X: D8 u: R+ {
//函数 从串口缓冲区取数据,只取前8个字节的数据
3 _9 k( c: m, {! B$ H: Astatic void EnterRxCmdModeByPc(unsigned char c)4 F# [- |7 F' j1 b7 l, [1 I7 A
{
% a8 C; l% a: C* h2 ? if(RxCmdPt < 8)
! u8 X2 \1 T5 H, W4 q6 k {% }7 ~/ g" y( E: x( i
RxCmdTimeOut = 10;- O2 I1 K8 q0 d
uartGetBuf[RxCmdPt] = c;
% y8 v* D3 }9 Y5 L if(memcmp(uartGetBuf,Updat,3)==0)
/ ^2 h/ ^$ N+ }& Z5 l/ ^ {
% g |- U- f# o$ A' K G EnterIspMode();
& c9 }5 S% t5 H) D' D }
7 m. J/ o/ s1 D# a" { RxCmdPt ++;/ a1 j6 ~1 \+ K/ |) b( F$ @
}1 O7 R, b0 `+ i) k6 V! h( N: v
}
" C- P1 i: A L4 Z- R% p6 D//函数 取缓冲区中的数据
& f5 A. y9 F/ E- Z//返回
' |2 L( ~- i7 @6 w3 n6 `extern uchar Getchar(void)- M/ o# I- i! L# r7 s" q6 [4 M) u/ A
{" K& ?* m' @7 t3 J+ R* S( d m
unsigned char c;
; g8 L5 q; o' N) E
( t5 h' h% d" u2 g( O6 z5 W/ ~ c = uart_rx_buf[uart.rx.tail];: O- s, @, C# x" P' ?
EnterRxCmdModeByPc(c);
2 E2 {" Q+ y+ }! D5 Z1 i uart.rx.tail = RBUF_NEXT_PT(uart.rx.tail,sizeof(uart_rx_buf));2 l" z# A6 y+ Q# t! C
uart.rx.timeout = 100;6 z4 n5 e0 S2 W9 b
return c;# Y* h/ A) R7 L5 e) y+ o3 K
}: g8 R/ m! X' y5 m: O3 D% C0 T
//
, V; @ ~ z* F: vextern void UartTimer(void)using 1
( V% O4 |2 p- K2 c{
: g% Q4 v- y: A! A if((uart.rx.timeout)&&(--uart.rx.timeout == 0))! d. z# G" I1 O* ]0 X J
{% t. J& |0 c$ }- P7 q1 m
uart.rx.error = 1;
; I# Z# Y% Q: k n8 c! k. B& S }
5 M% x4 F% ]6 z8 g}! d' q) i8 Y; C6 q
//0 j4 V( `. ?" [! k9 ^+ D7 P6 F( O& Q
extern void ClearRxCmdPt(void)
; M5 n+ R& l7 U. x/ [{
- \8 l/ P/ q; m& M- ]+ ` if((RxCmdTimeOut)&&(--RxCmdTimeOut==0))
) U9 c# `( H0 |$ a3 |* [ k {
: h' x& q0 D6 c8 H- P% m RxCmdPt = 0; z: O! T B6 x7 r N. y$ t
}- n& {, u/ I) M0 X5 a+ I
} |
|