Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
******/
/* Example of Asyncronous Serial Comms on a PIC16F877
*/
/*
*/
/* By: J.Winpenny
*/
/*
*/
/*--------------------------------------------------------------------------------
-----*/
/* Date: | Revision History
*/
/*--------------------------------------------------------------------------------
-----*/
/* 16/11/1999 | Original Coding
*/
/* |
*/
/* |
*/
/* 25/1/2000 | Revised for C2C 3.28
*/
/* |
*/
/* 25/1/2003 | Bug fix. Spotted by Jussi Sainio, jusic@mbnet.fi
*/
/* |
*/
/* 8/3/2003 | Removed comment after pragma ( line 37 )
*/
/* |
*/
/*--------------------------------------------------------------------------------
-----*/
/* Target: PIC16F877
*/
/*
*/
/* Baud: 9600
*/
/* Bits: 8
*/
/* Parity: none
*/
/* Stop bits : 1
*/
/*
*/
/* Warning: Clock frequency dependant
*/
/*
*/
/*
*/
/* This example sends a message then
*/
/* echos lines typed on a terminal.
*/
/*
*/
/*********************************************************************************
******/
// My PORT Configuration
#define TRMT_MASK 2
// Bits of SSPSTAT
#define SMP 7
#define CKE 6
#define D_A 5
#define P 4
#define S 3
#define R_W 2
#define R_W_MASK 0x04
#define UA 1
#define BF 0
// Bits of SSPCON2
#define GCEN 7
#define ACKSTAT 6
#define ACKDT 5
#define ACKEN 4
#define RCEN 3
#define PEN 2
#define RSEN 1
#define SEN 0
// Bits of PIR1
#define PSPIF 7
#define ADIF 6
#define RCIF 5
#define TXIF 4
#define SSPIF 3
#define SSPIF_MASK 0x08
#define CCP1IF 2
#define TMR2IF 1
#define TMR1IF 0
// Bits of SSPCON
#define WCOL 7
#define SSPOV 6
#define SSPEN 5
#define CKP 4
#define SSPM3 3
#define SSPM2 2
#define SSPM1 1
#define SSPM0 0
// Port addresses
char PORTC@0x07;
char PORTD@0x08;
char PORTE@0x09;
// USART Registers
char TXREG@0x19;
char RCREG@0x1a;
char TXSTA@0x98;
char RCSTA@0x18;
char SPBRG@0x99;
// Other regs
char PIE1@0x8c;
char PIE2@0x8d;
char PIR1@0x0c;
char PIR2@0x0d;
char PCON@0x8e;
// ADC bits
char ADCON0@0x1f;
char ADCON1@0x9f;
// Function Declarations
#define RX_BUFFER_SIZE 20
//--------------------------------------------------------------------------------
---
// Start of MAIN
//--------------------------------------------------------------------------------
---
void main(void)
{
BufferIndex = 0;
while( 1 )
{
//!!!!!!!!!!!!!!!!!!!!!!!!!!!
// Start revision 25/01/03
BufferIndex = 0;
clear_wdt();
} // end of Main()
void interrupt(void)
{
/*****************************************************/
/* Send a const string */
/* ( Null terminated ) */
/*****************************************************/
char i;
i = 0;
// Check for end of string
while( ptr[i] != 0 )
{
SendChar( ptr[i++] );
}
/*****************************************************/
/* setup PIC16F877 options,ports,interrupts */
/*****************************************************/
void Setup(void)
{
INTCON = 0x00;
set_bit( INTCON, GIE ); // Enable Global Interrupts
set_bit( INTCON, PEIE ); // Enable all Peripheral Interrupts
TRISD = PortDConfig;
ADCON1 = 0x7f; // Disable ADC
TRISA = PortAConfig;
TRISB = PortBConfig;
TRISC = PortCConfig;
TRISE = PortEConfig;
PIR1 = 0;
clear_wdt();
/*******************************************************/
/* Configure USART for communications */
/* */
/* Asynchronous mode */
/* 19,200 Baud ( With 3.579545 Mhz Clock ) */
/* 8 data bits ( For other rates see PIC16F8XX Data ) */
/* 2 stop bits */
/* No Parity */
/* */
/*******************************************************/
void ConfigureComms(void)
{
set_bit( RCSTA, SPEN ); // Enable Serial port
clear_bit( RCSTA, RX9 ); // 8 bit receive mode
//------------------------------------------
// For this value at a given clock rate
// see the microchip document
//------------------------------------------
/*****************************************************/
/* Send a character over the RS232 Port */
/* */
/* */
/*****************************************************/
void SendChar(char ch)
{
char TxEmpty;
clear_wdt();
}
/*****************************************************/
/* Receive a character over the RS232 Port */
/* */
/* Called from Interrupt service routine */
/* */
/* Returns the char received */
/* and saves it in the buffer */
/* */
/*****************************************************/
char RxChars(void)
{
if ( ( RCSTA & 6 ) == 0 ) // Then if no errors
{ // Process received character
// ...
return RCREG;