📄 serial.c.bak
字号:
/*
Interface PIC
Designed by Aminigo Hsieh Nov 2007.
Microprocessor: Microchip PIC16F87x PIC16F88x
Compiled with: Hitech-C v9.6, developed using MPLAB v7.6
Note: all references are to PIC16C7X PDF version of Microchip manual, DS30390E
Overall goal: serial comms using USART to comm port of an ibm pc compatible computer
*/
#include <pic.h>
#include <conio.h>
#include <stdio.h>
#include "always.h"
#include "delay.h"
extern const unsigned char HexToAsc_Table[];
//extern unsigned char UtcH,UtcM,UtcS;
void serial_setup(void)
{
/* relates crystal freq to baud rate - see above and PIC16F87x data sheet under 'USART async. modes'
BRGH=1, Fosc=3.6864MHz BRGH=1, Fosc=4MHz BRGH=1, Fosc=8MHz BRGH=1, Fosc=16MHz
---------------------- ----------------- ----------------- ------------------
Baud SPBRG Baud SPBRG Baud SPBRG Baud SPBRG
1200 191 1200 207.3 1200 415.7 9600 103
2400 95 2400 103.2 2400 207.3 19200 51
4800 47 4800 51.1 4800 103.2 38400 25
9600 23 9600 25.0 9600 51.1 57600 16
19200 11 19200 12.0 19200 25.0 115200 8
38400 5 38400 5.5 38400 12.0
57600 3 57600 3.3 57600 7.7
115200 1 115200 1.2 115200 3.3
*/
/*
* Comms setup:
*/
#define BAUD 9600
#define DIVIDER ((PIC_CLK/(16UL * BAUD) -1))
#define HIGH_SPEED 1
SPBRG=DIVIDER;
BRGH=HIGH_SPEED; //data rate for sending
SYNC=0; //asynchronous
SPEN=1; //enable serial port pins
CREN=1; //enable reception
SREN=0; //no effect
TXIE=0; //disable tx interrupts
RCIE=0; //disable rx interrupts
TX9=0; //8-bit transmission
RX9=0; //8-bit reception
TXEN=0; //reset transmitter
// TXEN=0; //enable the transmitter
}
unsigned char dummy;
#define clear_usart_errors_inline \
if (OERR) \
{ \
TXEN=0; \
TXEN=1; \
CREN=0; \
CREN=1; \
} \
if (FERR) \
{ \
dummy=RCREG; \
TXEN=0; \
TXEN=1; \
}
//writes a character to the serial port
void putHex(unsigned char c)
{
while(!TXIF) //set when register is empty
{
clear_usart_errors_inline;
CLRWDT();
}
TXREG=(HexToAsc_Table[(c/0x10)]);
while(!TXIF) //set when register is empty
{
clear_usart_errors_inline;
CLRWDT();
}
TXREG=(HexToAsc_Table[(c&0xf)]);
//DelayUs(10);
}
void putch(unsigned char c)
{
while(!TXIF) //set when register is empty
{
clear_usart_errors_inline;
CLRWDT();
}
TXREG=c;
DelayUs(10);
}
void putbuf(unsigned char cnt,unsigned char *p)
{
unsigned char i,c;
for(i=0;i<cnt;i++)
{
c=*p;
// putch(*p);
p++;
}
}
void putst(register const char *str)
{
while((*str)!=0)
{
putch(*str);
if (*str==13) putch(10);
if (*str==10) putch(13);
str++;
}
}
//-----------------------------------------------------
void SendToPc(void)
{
putch('$');
putHex(OldKeyStatus);
putHex(KeyStatus);
putHex(VR_Value);
putHex(UtcH);
putHex(UtcM);
putHex(UtcS);
putch('*');
/*
putHex(GpsLock);
putch(',');
putHex(IntJDValue[0]);
putHex(IntJDValue[1]);
putch('.');
putHex(FloatJDValue[0]);
putHex(FloatJDValue[1]);
putHex(FloatJDValue[2]);
putch(',');
putch(GpsJD_a);
putch(',');
putHex(IntWDValue);
putch('.');
putHex(FloatWDValue[0]);
putHex(FloatWDValue[1]);
putHex(FloatWDValue[2]);
putch(',');
putch(GpsWD_a);
putch(',');
putHex(KeyStatus);
putch(',');
putHex(SpeedSet);
putch(',');
putch(UTCtime[0]);
putch(UTCtime[1]);
putch(UTCtime[2]);
putch(UTCtime[3]);
putch(UTCtime[4]);
putch(UTCtime[5]);
putch('*');
*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -