Pic 16F913 UART communications setup
I've attached my code below (I'm blinking a LED to be able to see that the loop is occurring BTW).
I am not getting any data sent out over the UART, I have the Pic 16F913, and pin 17 is the output I'm trying to get, I have it hooked up to a RF-42N bluetooth module, I know the module works, because if I connect the rx to the tx, I get an echo. I then tried the following setup to send the letter "z" to the bluetooth module (and hopefully read on my phone, using blueterm). I am getting no data, I don't know what the default FOSC is set to, so I've tried various values for the SPBRG hoping that i would be able to "guess" the right one, but none of them have seemed to work.
I'm using the Hi-Tech C compiler (Free version).
I hope this is enough information for everyone, I just have no clue what I am missing, I"ve been fighting this for hours.
// Main file
#include <uart.h>
__CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT & UNPROTECT & BORDIS & IESODIS & FCMDIS);
int i, j;
void wait(int z);
int main()
{
PORTA = 0x0;
CMCON0 = 7;
// Turn off Comparators
ANSEL = 0;
// Turn off ADC
// Trisa4/5 0's mean output, 1's mean input
TRISA4 = 0;
// Make RA4/RA5 Outputs
TRISA5 = 0;
setupAsyncUart();
while (1==1)
{
RA4 = 0;
send('Z');
wait(100);
RA4 = 1;
wait(100);
}
}
// Wait routine.
void wait(int z)
{
for (int a=0; a<z; a++)
{
for (int b=0; b<z; b++)
开发者_高级运维 {
}
}
}
// uart.h
void send(char string)
{
TXREG = string;
}
void setupAsyncUart(int BAUDRATE)
{
SPBRG = 10;
BRGH = 1; //Low speed = 0 high speed = 1
SYNC = 0;
SPEN = 1;
TXEN = 1;
}
First off all you must to set the internal oscillator to appropriate CPU frequency.
So set first in __CONFIG register bits FOSC<2:0> = (100) to select internal oscillator, after that set at very beginning of program the bits IRCF2, IRCF1 and IRCF0 in OSCCON register to desired oscillator frequency, default frequency is 4Mhz.
Than init the UART
- Initiate CPU pins because some pins are shared with other MCPU periphery.
- Load into SPBRG register proper number depend on your baud rate and CPU clock frequency (check datasheet).
- Set BRGH bit in register TXSTA depend on desired baud rate generator (check datasheet).
EDIT:
To config internal clock use:
#include <htc.h>
__CONFIG(INTIO)
You should find all other bit declarations in "pic16f91x.h" file.
// Configuration Mask Definitions
#define CONFIG_ADDR 0x2007
// Oscillator
#define EXTCLK 0x3FFF // External RC Clockout
#define EXTIO 0x3FFE // External RC No Clock
#define INTCLK 0x3FFD // Internal RC Clockout
#define INTIO 0x3FFC // Internal RC No Clock
#define EC 0x3FFB // EC
#define HS 0x3FFA // HS
#define XT 0x3FF9 // XT
#define LP 0x3FF8 // LP
// Watchdog Timer
#define WDTEN 0x3FFF // On
#define WDTDIS 0x3FF7 // Disabled / SWDTEN control
// Power Up Timer
#define PWRTDIS 0x3FFF // Off
#define PWRTEN 0x3FEF // On
// Master Clear Enable
#define MCLREN 0x3FFF // MCLR function is enabled
#define MCLRDIS 0x3FDF // MCLR functions as IO
// Code Protect
#define UNPROTECT 0x3FFF // Code is not protected
#define CP 0x3FBF // Code is protected
#define PROTECT CP //alternate
// Data EE Read Protect
#define UNPROTECT 0x3FFF // Do not read protect EEPROM data
#define CPD 0x3F7F // Read protect EEPROM data
// Brown Out Detect
#define BORDIS 0x3CFF // BOD and SBOREN disabled
#define SWBOREN 0x3DFF // SBOREN controls BOR function (Software control)
#define BORXSLP 0x3EFF // BOD enabled in run, disabled in sleep, SBOREN disabled
#define BOREN 0x3FFF // BOD Enabled, SBOREN Disabled
// Internal External Switch Over Mode
#define IESOEN 0x3FFF // Enabled
#define IESODIS 0x3BFF // Disabled
// Monitor Clock Fail-safe
#define FCMEN 0x3FFF // Enabled
#define FCMDIS 0x37FF // Disabled
// In-Circuit Debugger Mode
#define DEBUGEN 0x2FFF // Enable ICD2 debugging
#define DEBUGDIS 0x3FFF // Disable ICD2 debugging
OSCCON definition you should find in "cas16f913.h" file...
OSCCON equ 008Fh
#define SCS_bit BANKMASK(OSCCON), 0
#define LTS_bit BANKMASK(OSCCON), 1
#define HTS_bit BANKMASK(OSCCON), 2
#define OSTS_bit BANKMASK(OSCCON), 3
#define IRCF0_bit BANKMASK(OSCCON), 4
#define IRCF1_bit BANKMASK(OSCCON), 5
#define IRCF2_bit BANKMASK(OSCCON), 6
Just to be sure that your TX function will work, use it this way :
void vTxChar (unsigned char ucByte)
{
while (!TXIF); //Waits for previous transfer to be done
TXREG = ucByte; //Loads TXREG with your value
}
This while loop checks the transmit interrupt flag (even if not set) to see if the TXREG is empty or still set with previous value.
Depending on your XTAL frequency, your SPBRG value will vary, calculate it correctly then check with a computer if you have the right baudrate value.
16MHZ, BRGH=1, your SPBRG value for 9600 BDS = 0x68.
精彩评论