Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
main.c:72: warning: (752) conversion to shorter data type
main.c:72: warning: (751) arithmetic overflow in constant expression
main.c:82: warning: (752) conversion to shorter data type
main.c:86: warning: (752) conversion to shorter data type
main.c:88: warning: (752) conversion to shorter data type
main.c:98: warning: (752) conversion to shorter data type
main.c:98: warning: (751) arithmetic overflow in constant expression
main.c:104: warning: (752) conversion to shorter data type
main.c:105: warning: (752) conversion to shorter data type
main.c:108: warning: (752) conversion to shorter data type
main.c:112: warning: (752) conversion to shorter data type
main.c:104: warning: (759) expression generates no code
//____________________________________________________
//
// Soft UART Test for PIC16F505
// Based on http://www.romanblack.com/bitbangserial.htm
//
// C.A.Roper - 2013/02/23
//____________________________________________________
// CONFIG
#pragma config OSC = ExtRC_CLKOUTEN// Oscillator Selection bits (External RC oscillator/CLKOUT function on RB4/OSC2/CLKOUT pin)
#pragma config WDT = OFF // Watchdog Timer Enable bit (WDT enabled)
#pragma config CP = OFF // Code Protection bit (Code protection off)
#pragma config MCLRE = ON // RB3/MCLR Pin Function Select bit (RB3/MCLR pin function is MCLR)
// #pragma config statements should precede project file includes.
#include <xc.h>
#include <stdint.h>
//#include <pic.h>
//
// Use project enums instead of #define for ON and OFF.
// Prototypes of functions used
//
void send_serial_byte(unsigned char);
unsigned char receive_serial_byte(void);
//
// Global Defines
//
#define _XTAL_FREQ 4000000 // System Clock rate
#define BAUDRATE 9600 // Desierd BAUD Rate (tested) Rates 9600 and 19200
#define SER_BAUD ((_XTAL_FREQ / 4) / BAUDRATE - 3)
#define SER_BIT 0 // Signal MODE - 1 = Normal 0 = Inverted (Use Inverted for direct 232)
//
// I/O Pins
//
#define TxPin PORTCbits.RC0 // Output Tx
#define RxPin PORTCbits.RC1 // Input Rx
//
// MAIN
//
void main(void)
{
//
// Setup - Run once
//
// Default I/O Setup
//
OSCCAL= 0b01111110;
PORTC = 0; // Turn all Outputs OFF
TRISC = 0b001010; // INPUTS: GP1 = RxPin, GP3 = Button
OPTION = 0b11110110;
//
// Main Loop - Run Forever
//
while(1)
{
if(RxPin == !SER_BIT) // wait here for serial byte start bit
{
send_serial_byte(receive_serial_byte()); // Echo back
}
}
}
//
// User Functions
//
void send_serial_byte(unsigned char data)
{
unsigned char i;
i = 8; // 8 data bits to send
TxPin = !SER_BIT; // make start bit
TMR0 = (256 - SER_BAUD); // load TMR1 value for first baud;
while(TMR0 & (1 << 7)); // wait for baud
while(i) // send 8 serial bits, LSB first
{
if(data & 1<<0)TxPin= SER_BIT; // send data bit
else TxPin=!SER_BIT;
data = (data >> 1); // rotate right to get next bit
i--;
TMR0 -= SER_BAUD; // load corrected baud value
while(TMR0 & 1<<7); // wait for baud
}
TxPin = SER_BIT; // make stop bit
TMR0 -= SER_BAUD; // wait a couple of baud for safety
while(TMR0 & 1<<7);
TMR0 -= SER_BAUD;
while(TMR0 & 1<<7);
}
unsigned char receive_serial_byte(void)
{
unsigned char RxChr; // holds the serial byte that was received
unsigned char i; // Bit Index
i = 8; // 8 data bits to receive
TMR0 =(256 - (SER_BAUD - 19)); // load TMR1 value to offset ~center of RxBit
while(TMR0 & 1<<7); // wait for baud
while(i) // receive 8 serial bits, LSB first
{
RxChr = (RxChr>>1); // rotate right to store each bit
if( RxPin == 1) // save data bit
RxChr = RxChr | SER_BIT<<7;
else RxChr = RxChr |!SER_BIT<<7;
i--; // Next Bit
TMR0 -= SER_BAUD; // load corrected baud value
while(TMR0 & 1<<7); // wait for baud
}
TMR0 -= SER_BAUD; // wait for stop bit, ensure serial port is free
while(TMR0 & 1<<7);
return RxChr;
}