|
#include "Include.h"
# G h- i4 S6 y/ v1 g' l9 _
2 Q9 g8 t# g7 r* l {unsigned char xdata uart_rx_buf[128];! y* ?$ U# f5 w/ N" g* z
unsigned char xdata uart_tx_buf[128];
/ I7 y& ?+ Y1 Vvolatile UART_T xdata uart;- ^! z0 c5 t* y* T4 {1 r2 _& W/ d
unsigned char xdata uartGetBuf[8];5 |6 S. n6 r3 m5 M2 P
unsigned char RxCmdPt,RxCmdTimeOut;( ?: Z' c o) Z c$ f, d) y n
7 _# `1 N0 s* g1 D( }static void Serial0(void)interrupt 4 using 1
: D! {/ D* t( F! o/ Q{
$ h$ t9 C2 C2 \1 R unsigned char temp,head;# j) ~- A8 y5 P
0 ~4 s- I+ ]% @0 t4 ?, F if(_testbit_(RI))
- `" E# W2 X/ F0 D; ]5 e$ U- a {
% s4 E. D& |, E! V* z: Q: [8 N temp = SBUF;
3 a9 x: I/ u5 A1 q* Q5 N, @/ i head = RBUF_NEXT_PT(uart.rx.head,sizeof(uart_rx_buf));
- f) Z# T: K: w- G5 R if(head != uart.rx.tail)( }; r( h' h; S! w6 r
{
, l/ L1 e! _# e4 T5 x4 ` uart_rx_buf[uart.rx.head] = temp;1 m1 h4 _- w" S1 _1 W" _" P
uart.rx.head = head;
5 E8 W& K3 C2 _9 W% t% a }else8 a. m3 s9 d1 S* v' G$ M7 D
{1 Z! R- k( E- U
uart.rx.error = 1;2 q. ~% s/ R% {
}
) g e6 j5 [) O! @, E# ?: w0 i4 r7 ^ }
- z) P9 w& H" [0 } if(_testbit_(TI))
* o' `0 a1 E; |7 g {1 O2 R/ p: v6 z
if(uart.tx.head == uart.tx.tail)
9 h! [+ Z1 [% g8 L0 v. D {
6 j7 z+ b# ` n+ y4 s! t0 l uart.tx.busy = 0;
5 u9 b8 I9 a7 }1 v; |- J }else
" r" V% b1 V' ]3 T+ h! m {
$ ?4 g3 w j8 o. j: V3 V" Y SBUF = uart_tx_buf[uart.tx.tail];
, M- d3 N2 D0 T9 b! x uart.tx.tail = RBUF_NEXT_PT(uart.tx.tail,sizeof(uart_tx_buf));
0 r! @. l) I: n# t8 _0 M }/ E, o9 V, u# s8 \* K: h
}* L: ^4 `+ h$ z9 c- }
}% f" ^! c! _! M7 t2 P) F& g
//函数 串口中断初始化, ~7 z3 B/ t6 m
extern void UartInit(void)" n7 ^) ]# T: K, Y. b3 T0 A
{
# u0 Q# H& w* F7 p( s8 { RCAP2L = -(XTAL/32/BaudRate_9600);
; l# L" K5 ^( R r8 D RCAP2H = (-(XTAL/32/BaudRate_9600))>>8;
3 A, X6 X& i1 q( [* d4 d" J+ J* Z! Y4 |4 p
T2CON = 0x34;/ y, o, _; j- l; u- \2 P
SCON = 0x50;
1 M0 S/ D6 A% C, C2 M
) X; k# }3 Y; W; m IP |= 0x10; //serial 0 high priority
" a0 `4 T6 T! u1 {6 m1 L# r IE |= 0x10; //enable serial 0 {& L. T! m+ Y- r% V
, q2 q: |: p$ I* | memset(&uart,0,sizeof(uart));) t( P3 ^* @) k& s+ \
}( t3 B* e) K0 J4 J1 h* Y
// put char
3 h/ ] u" t$ n* f! O9 Nextern char Putchar(uchar c)' n. W, Z7 l" w/ f% C, d
{: F* G* Z1 s4 g1 P
unsigned char head;
- |: e+ F4 P/ _3 n5 a2 Y
; V6 V0 S0 P- Y8 G head = RBUF_NEXT_PT(uart.tx.head,sizeof(uart_tx_buf));2 e9 h/ O8 s! k, ~0 A% Z5 N
uart_tx_buf[uart.tx.head] = c;; C( W4 l$ B$ U% H5 t
uart.tx.head = head;6 @ e. P8 ]0 Y8 _
6 D7 ~/ t3 r. r& `8 L8 S& F8 J if(uart.tx.busy != 0x5a)
0 q. T0 U$ T- a/ k+ k( ?" u {
: S2 P1 U1 K9 m0 v' W uart.tx.busy = 0x5a;" r* N. X* R/ L7 G
TI = 1;# u1 \; f* W$ r5 [ R
}4 O1 q, m1 z4 X5 v+ U" f
return c;4 A# n' p5 t& }/ z @
}
% `9 P$ [4 N' C! g//函数 判断串口是否有数据传进来" z/ S& q0 K! c' n4 r- R/ F
// 如果有,进行接收数据
T9 V }5 U+ x; b/ K5 @4 Yextern uchar GetRxBufSize(void)
7 F7 G1 u* w+ Q& y$ ]9 ~4 z{8 w! W2 g7 T: e( t4 |( S/ a0 s8 j" _ C; \
unsigned char head;9 s+ \. o5 E& d& ^
5 H4 g r: j4 w- M* Z' J0 h, n1 y* a, s
head = uart.rx.head; A9 } W2 n: M8 |
if(head < uart.rx.tail); }; P7 O0 W3 ^- q3 b; X
{% T. h, N' g& j, W+ m
return (sizeof(uart_rx_buf) - (uart.rx.tail - head));
. a9 W e$ I& C( z" E# b; P }else
) {( h" `/ U9 J7 d8 t0 _ {% [7 ^0 K$ F: h- |1 [; r3 z) d
return (head - uart.rx.tail);- Y! ?2 n: z$ I7 u
}
; K9 \( U+ D" I4 W}8 a2 n4 p5 Y& U- F- h4 \- x2 l
//函数 从串口缓冲区取数据,只取前8个字节的数据
# B! ^" Z2 [3 t7 e5 G0 d4 _! mstatic void EnterRxCmdModeByPc(unsigned char c)
4 u3 W/ `( E5 A' B; c{9 s* @/ J! F, R4 L+ N c; o$ s H5 x
if(RxCmdPt < 8)
, z: x w6 Y- }9 i {
, P( N' x& }' O% q7 l: ~1 b& f% G RxCmdTimeOut = 10;2 W7 _: N' r2 P! J5 u: Z5 Q
uartGetBuf[RxCmdPt] = c;- q% B, z* K- K9 N" p' \; K: P
if(memcmp(uartGetBuf,Updat,3)==0)
* a8 @% D/ f7 a Z" P) t; M# C {0 N5 c" i9 f0 u. z9 x$ t W
EnterIspMode();# r+ [" o3 X- x; b6 j8 v9 Y1 S
} P5 X: S. \3 x/ T$ J
RxCmdPt ++;
3 X. [" q5 F E0 s2 x6 F }
0 L7 D, m( D* ?" R Q. G0 N4 L}
2 R' i( @; b, @' @$ b3 A//函数 取缓冲区中的数据' {. r9 l/ s; w) g
//返回
8 |8 e8 G; Y) u4 s. p2 J5 vextern uchar Getchar(void)6 C Y+ q# P" d+ T) U1 K$ D
{
e+ p, o- h) Q, i) S unsigned char c;
; t, j7 Z5 O" o4 X+ @+ |2 ^
3 I: T! x2 ]; q% `$ X6 X: h c = uart_rx_buf[uart.rx.tail];
2 C$ s& S! N3 b0 g EnterRxCmdModeByPc(c);: \0 U. q ~. P" \8 |
uart.rx.tail = RBUF_NEXT_PT(uart.rx.tail,sizeof(uart_rx_buf));2 T1 ?( x' N. \
uart.rx.timeout = 100;
2 o n2 Y! C* v3 G7 q' X return c;: F4 Z4 a X. I; }# a4 Q& W7 @* H2 b( l2 J
} m( Y R" J2 C, ?+ v
//
8 c5 n8 T' @# O1 |! Gextern void UartTimer(void)using 1
) Y( I/ d) j" o( f4 ^' d: q{) K# b8 K1 e5 N* Y4 K" F
if((uart.rx.timeout)&&(--uart.rx.timeout == 0))% {. t" o" \) ?2 d: H2 \
{5 H% C3 Q2 C4 V) d9 V( }8 w
uart.rx.error = 1;. |8 Q8 @4 b5 y
}
2 y. P" ^; L4 E6 F, G2 D/ t}
$ B! H2 g2 L' \! d//
& W& W" { h' R- rextern void ClearRxCmdPt(void), s$ F1 I7 K. \0 m
{! ~; f" Y& c) k; B0 r3 ?
if((RxCmdTimeOut)&&(--RxCmdTimeOut==0))4 L$ V9 c* U% [% H8 `2 i$ T/ a. @
{! e1 I+ \. i# A9 i( d N; G7 R, x5 N5 }
RxCmdPt = 0;
8 L+ V( Z& U6 N( N/ _6 E }5 v5 J/ v/ \/ T; ~( _; t5 O
} |
|