|
#include "Include.h"# }; W7 _8 G4 a# y
( r7 B! [% S& F; m, k- N. qunsigned char xdata uart_rx_buf[128];
: h3 E, `9 {$ c- Z* m j8 [unsigned char xdata uart_tx_buf[128];% \) X- n& t; Z W0 ?
volatile UART_T xdata uart;
8 ~ q' X: t+ l$ @( s+ R3 X) Lunsigned char xdata uartGetBuf[8];; R0 X* W9 b6 x8 f
unsigned char RxCmdPt,RxCmdTimeOut;
5 Z9 {) T$ H1 f) ?7 w, U) e! z! j+ m, P1 Z$ R6 ~
static void Serial0(void)interrupt 4 using 1: `+ a M0 @% ~4 a7 v! H
{
1 W! x7 t: u4 K7 Q- Z9 N3 i/ ? unsigned char temp,head;/ g) A9 `5 t" [/ U7 n
& J# b8 p, y! @1 X' Z- ~4 W" ] if(_testbit_(RI))
- _5 _& R, F# I0 }& i; j5 I, ] {
/ l0 ^4 M1 @0 G" m- K$ T1 U! r temp = SBUF;/ L( s; k% A: U% Q8 }
head = RBUF_NEXT_PT(uart.rx.head,sizeof(uart_rx_buf));* v! a0 f5 Q0 u
if(head != uart.rx.tail): O* g) Z6 @# e6 ~7 l
{
5 j3 ^7 B1 k; \6 ~4 O# @! d. Y% R7 d1 J uart_rx_buf[uart.rx.head] = temp;/ w% m. m5 d5 D( I7 ~' \
uart.rx.head = head;
% q( J( E7 V# q3 i0 _8 z }else
) e5 m F- r: R, s; q) K! T {
: D6 X+ b" g1 F1 ?3 V uart.rx.error = 1;
$ V4 F0 B N. U9 Y2 w* \5 Y }
- A2 O% h* O+ L5 r }
3 Z/ b4 E: F7 l( X5 ^/ H; q if(_testbit_(TI))& |* Y. o9 y) f5 V) b
{
, k! {3 ?3 O- d' O if(uart.tx.head == uart.tx.tail)& ?0 v5 Q: K `' P7 R* `$ N2 p
{; r1 \3 ^' F) I: P7 W/ i4 g$ i
uart.tx.busy = 0;
: r; I1 O D, o& |4 @ }else) A/ x- [) U9 u# Q3 |( N/ K
{: d# J' w- c1 J2 L7 W
SBUF = uart_tx_buf[uart.tx.tail];$ @: V3 a( r# D/ c: X+ O% ~
uart.tx.tail = RBUF_NEXT_PT(uart.tx.tail,sizeof(uart_tx_buf));
- {8 M# x5 ~: b- M! H6 l }& w: Y% z8 r, U1 M5 v7 [% ?
}
5 W9 M, p! I1 u1 _% t}
" @8 e* S) `( [//函数 串口中断初始化4 w) U0 e3 q' R
extern void UartInit(void)
8 P# {$ j) i( t& W# ~' L; q) w{
2 t/ P- {7 X. j4 g RCAP2L = -(XTAL/32/BaudRate_9600);
) X3 R' U, c6 [% d# R1 |, K RCAP2H = (-(XTAL/32/BaudRate_9600))>>8;
4 b8 Y6 r5 Z* |. L
( }2 h( \( g9 o8 y9 d, V# Y T2CON = 0x34;$ m/ D$ {. o1 k: b9 |& b
SCON = 0x50;
/ Q9 U) b" l6 h; A
( C7 H3 L" j% k, p5 N: M* Z: ]9 j IP |= 0x10; //serial 0 high priority
. }. M R4 p' G a% b IE |= 0x10; //enable serial 08 u3 {2 O8 k) f( E
5 b3 \2 i0 U3 c9 M% `- n/ h memset(&uart,0,sizeof(uart));
# v. U' V* V7 c4 y$ r& v6 U( C9 ~}
' i) _0 |( o% B2 P+ A0 S: N// put char
/ w: `# e0 g& T( ~ n4 vextern char Putchar(uchar c)
2 g/ v1 _6 }9 a{
1 V5 G, l, o& _* ^9 G unsigned char head;
# w0 K% [% T5 H% O% F: N/ A
4 O+ A$ S6 e, p7 Y1 l+ A head = RBUF_NEXT_PT(uart.tx.head,sizeof(uart_tx_buf));2 `4 }% J" J! d- A
uart_tx_buf[uart.tx.head] = c;; A0 R7 ^; F4 @4 `# T" M
uart.tx.head = head;; `$ W; }* b( R4 P1 Q' f5 d
, H4 s5 D4 z5 _1 A% ^# G if(uart.tx.busy != 0x5a)& P% `5 z; u/ W, C
{0 ~; A/ o v2 @& J! n# ^" S
uart.tx.busy = 0x5a;) |; s% Q/ }, H
TI = 1;3 d, M) ~( L: A) z
}
( T# @$ d5 a- ^7 o3 }" `8 J/ E7 Y return c;
2 y& _' D& [1 I1 O1 _}+ L* z- t" S0 Z' m: f
//函数 判断串口是否有数据传进来 i! w/ y$ E3 ~+ N$ ~
// 如果有,进行接收数据
" J3 r" a3 B9 L# _' nextern uchar GetRxBufSize(void)
& q" p) ^8 x, z. e! \{" K* g b9 E. N
unsigned char head;
4 w* h/ B# u1 v, }6 Q3 C# A2 A
! J$ ~5 [$ T% p6 \9 z1 l% S s( W" Q head = uart.rx.head;( g" m0 @, q! \8 x% o- W: j( h
if(head < uart.rx.tail)4 P7 a. F: e+ ^5 K7 R3 N
{
( T! E, P& d7 G/ ^2 \6 R. ~/ J return (sizeof(uart_rx_buf) - (uart.rx.tail - head));
, j1 J0 A4 x. I }else
9 S) K5 ?9 T, I/ m4 G9 D {. v. y: A8 \* A
return (head - uart.rx.tail);
5 U4 y0 R9 ], Z1 t. H& ?3 H }# l0 d, z! K ]; {) b v
}
* `% F$ h% n* R( [( w//函数 从串口缓冲区取数据,只取前8个字节的数据
* m* b/ b; {, {% M# X% xstatic void EnterRxCmdModeByPc(unsigned char c)
; x( P4 t) S) [ u% D0 ~' K9 @1 v{
, h! {' q9 a- f% A4 v7 d if(RxCmdPt < 8)" |+ ?. S/ ]' @8 Q5 b7 T' v7 y
{) q9 C) U. ?+ o; v6 O! Y4 S( }
RxCmdTimeOut = 10;9 t0 g \" f6 a6 X" W) L0 i
uartGetBuf[RxCmdPt] = c;8 m# _0 V& S1 B9 E) R% x( [
if(memcmp(uartGetBuf,Updat,3)==0)
/ V6 L/ V7 Y3 z* ` {" x6 w8 b3 S* {0 C t; Q6 P" t% _+ h
EnterIspMode();; E. Z. x2 M9 \% g6 Y
}1 w1 W1 p- S! V5 t) I V6 O
RxCmdPt ++;
+ f* [: O' R% Z' e }
6 T0 @. E" c# u/ z1 r}
# P1 O% ~, N) P% j; n//函数 取缓冲区中的数据3 z) Y" l4 |2 g- B. `
//返回) z7 A3 a. i: _% F' ?3 [ w
extern uchar Getchar(void)( F. p1 Y( R/ M+ X* p
{' E4 ^2 ~) J" a4 ~1 W, K
unsigned char c;6 N2 P. I* a: O6 v" o. A0 Z7 V: ]
* S# _- l' e* b- o2 O0 v! R- V c = uart_rx_buf[uart.rx.tail];, h2 ?+ \# p. f1 l# x
EnterRxCmdModeByPc(c);
# L; n+ g c8 J uart.rx.tail = RBUF_NEXT_PT(uart.rx.tail,sizeof(uart_rx_buf));+ U, e+ `' o, q% ^5 o* \: I9 l
uart.rx.timeout = 100;
3 F, ^. C9 Z7 \! f# i9 [! V: W return c;
2 B3 ^9 a7 C) Y5 ~}8 Z' o+ K o3 C1 U
//% @2 G7 w1 ^, t1 O/ W
extern void UartTimer(void)using 1
. z, u& J3 I0 Y{
& ~1 }) G3 g0 b; S' _ if((uart.rx.timeout)&&(--uart.rx.timeout == 0))
% O6 N( d; j! _6 u7 ^2 n {
3 ^* E2 l) g2 `4 I& y! M; L uart.rx.error = 1;8 ]+ ?( D8 l3 V
}- D1 c7 ^/ C- r& A! D9 S
}
& a. x1 o- L, _) L \//
. h, E( l+ s" m: Oextern void ClearRxCmdPt(void)
; h! ^ {+ I4 p5 l* E1 u{
5 b+ u6 \$ E0 W- q if((RxCmdTimeOut)&&(--RxCmdTimeOut==0))( I4 T f" @* P4 L# V
{
, X) @$ g$ X- ^ RxCmdPt = 0;
) O; E. d; \; ^ }
+ ^8 d3 ~& B M8 h( |6 G* z# ]} |
|