📄 code.c
字号:
////////////////////////////////////////////////////////////////////////////// Serial Communications Test Program//// Exercises the serial driver code in <rs232_driver.h> by reading and// writing to the serial port////////////////////////////////////////////////////////////////////////////// Author(s): Andrew Smallridge// Date 17 November 2004//// Copyright (c) 2004-2005 Andrew Smallridge// Copyright (c) 2004-2005 Pavel Baranov// Copyright (c) 2004-2005 David Hobday// Initialially developed on the PIC18F452////////////////////////////////////////////////////////////////////////////#pragma CLOCK_FREQ 20000000#include <system.h>////////////////////////////////////////////////////////////////////////////// The following three sections have been copied from the serial_driver.h file// USART software implementation template arguments and variables// USART hardwareware implementation template arguments//////////////////////////////////////////////////////////////////////////////// **** START OF DEFAULTS USED FOR SOFTWARE USART EMULATION ****//// RAM used by the software USART driver to emulate the equivalent serial hardware registers// Note this section is really just reserving the space. The defines in the subsequent section// overlay these RAM locations unsigned short sw_SPBRG@0x47; // define location for the emulated SSPCON1unsigned short sw_RCREG@0x48; // define location for the emulated SSPCON2unsigned short sw_TXREG@0x49; // define location for the emulated SSPSTATunsigned short sw_TXSTA@0x4A; // define location for the emulated SSPBUFunsigned short sw_RCSTA@0x4B; // define location for the emulated SSPADDunsigned short sw_TXIF_PIR@0x4C;// define location for the emulated TXIF_PIR1unsigned short sw_RCIF_PIR@0x4C;// define location for the emulated RCIF_PIR1////////////////////////////////////////////////////////////////////////////// USART software implementation template argument values////////////////////////////////////////////////////////////////////////////// variables cannot be passed as template arguments. The following constants map// the PIC registers and software emulated USART RAM locations. These constants are// then used by the templated functions. When changing the address of an emulated// register the corresponding constant mapping must also be changed.// PIC18F defaults for software emulated USART support#define TX_PORT 0x06#define TX_TRIS 0x86#define TX_BIT 6#define RX_PORT 0x06#define RX_TRIS 0x86#define RX_BIT 7#define e_SPBRG 0x47#define e_RCREG 0x48#define e_TXREG 0x49#define e_TXSTA 0x4A#define e_RCSTA 0x4B#define e_TXIF_PIR 0x4C#define e_RCIF_PIR 0x4C#define e_TXIF_BIT 4#define e_RCIF_BIT 5 #define MODE (USART_reset_wdt)////////////////////////////////////////////////////////////////////////////// bit_time is used by the software emulated USART and is the number of CPU// instruction cycles per bit.//// bit_time = FOSC / 4 / BAUDRATE// The maximum value of bit_time currently limited to approximately 24000// and is determined by the maximum word size supported by the compiler.// this will change when 32 bit variables are supported.// At FOSC of 40MHz the minimum standard baud rate is 600 baud.// When using the software emulated USART functions ensure that the baud rate// can be realistically supported by the FOSC used. bit_time values below// 36 will likely lead to unreliable comms.//// The software UART takes 8 instruction cycles in a bit time loop. Therefore// the driver software divides the bit_time value by 8. If experimenting with// different values for bit_time use increments of 8 to guarentee a different// bit time is selected//////////////////////////////////////////////////////////////////////////////#define bit_time 86 // 115200 baud at 40MHz//#define bit_time 1041 // 9600 baud at 40MHz#define bit_time 521 // 9600 baud at 20MHz////////////////////////////////////////////////////////////////////////////// Serial Control Flag Bits used for the MODE parameter//// DO NOT REMOVE THE COMMENTS FROM THE FOLLOWING FLAGS// These flags are defined in serial_driver.h and are include here for information// only. These flags are used in the MODE parameter.//// The USART_invert flag is used to signal the software UART that the serial// input and output signals are inverted from normal RS232 communications.// Hardware implementations that use standard RS232 transievers// such as MS1488/MC1489 or MAX232 line drivers / receivers do NOT require this flag.//// The flags USART_HW and the USART_invert are mutually exclusive.//// Sample use: #define MODE (USART_reset_wdt | USART_HW)//////////////////////////////////////////////////////////////////////////////// define USART Mode bits//#define USART_HW 0x01 // specifies use the HW USART in the PIC//#define USART_invert 0x02 // invert the tx and rx bits//#define USART_reset_wdt 0x04 // reset the WDT while waiting for a character in getc()// **** END OF DEFAULTS USED FOR SOFTWARE USART EMULATION ****#include <rs232_driver.h>void main(){ // for the hardware UART the paramter passed is the divisor // the hardware usart enables dynamic reconfiguration of the baud rate // the software emulated USART baud rate is defined by constants in // serial_driver.h - this file alos contains the address mapping for // RAM required to support the software emulated USART uart_init(1,21); // set high speed divisor mode and divisor value puts("Hello, world"); while (1) { if (kbhit()) { putc(getc()); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -