USART without "USART.h"
Posted: Sun Aug 03, 2014 11:38 am
I have been reading the datasheet for 18F452. I am just trying to make a start on using the datasheets to program in C without using the (read.c) and (write.c).
- Code: Select all
#include <xc.h>
//PIC18F452
//Configurations and Fuses
#pragma config OSC = HS, OSCS = OFF,PWRT = ON,BOR = OFF
#pragma config BORV = 45,WDT = OFF,WDTPS = 1,CCP2MUX = OFF
#pragma config STVR = OFF,LVP = OFF,DEBUG = OFF
#pragma config CP0 = OFF,CP1 = OFF,CP2 = OFF,CP3 = OFF,CPB = OFF,CPD = OFF
#pragma config WRT0 = OFF,WRT1 = OFF,WRT2 = OFF,WRT3 = OFF,WRTB = OFF,WRTC = OFF,WRTD = OFF
#pragma config EBTR0 = OFF,EBTR1 = OFF,EBTR2 = OFF,EBTR3 = OFF,EBTRB = OFF
//PORTB pins.
int delayselect[9] = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};
//Timer values.
char offset[20] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};
int temp = 0;
int inside = 0;
int outside = 0;
int scanPin = 600;
int DelayA = 580;
int DelayB = 32;
void Initialization(void) {
LATA = 0x00;
LATB = 0X00;
LATC = 0X00;
LATD = 0X00;
LATE = 0X00;
TRISA = 0x00;
TRISB = 0x00;
TRISC = 0x80;
TRISD = 0x00;
TRISE = 0x00;
PORTA = 0x00;
PORTB = 0x00;
PORTC = 0x00;
PORTD = 0x00;
PORTE = 0x00;
}
void usart_setup(void) {
//TRANSMIT STATUS AND CONTROL REGISTER
TXSTAbits.TXEN = 1;
TXSTAbits.BRGH = 1;
TXSTAbits.TX9 = 0;
//RECEIVE STATUS AND CONTROL REGISTER
RCSTAbits.SPEN = 1; //Set
RCSTAbits.CREN = 1; //Set
//Peripheral Interrupt Enable bits
INTCONbits.PEIE = 0; //Clear
INTCONbits.GIE = 0; //Clear
PIR1bits.RCIF = 0; //Clear
PIE1bits.RCIE = 0; //Clear
SPBRG = 129;
}
void testup(void) {
PORTB = 0x01;
}
void main(void) {
Initialization(); //Set all device pins.
usart_setup();
TXREG = offset[2];
while (1) {
INTCONbits.GIE = 0;
if (PIR1bits.RCIF == 0)
Initialization();
testup();
}
}