#include <stdio.h>
#include <htc.h>
#include "usart.h"
#ifndef _XTAL_FREQ
// Unless specified elsewhere, 4MHz system frequency is assumed
#define _XTAL_FREQ 20000000
#endif
__CONFIG( HS & WDTDIS & PWRTDIS & BORDIS & LVPDIS & WRTEN & DEBUGDIS & UNPROTECT);
/* A simple demonstration of serial communications which
* incorporates the on-board hardware USART of the Microchip
* PIC16Fxxx series of devices. */
void putch(unsigned char byte)
{
/* output one byte */
while(!TXIF) /* set when register is empty */
continue;
TXREG = byte;
}
unsigned char
getch() {
/* retrieve one byte */
while(!RCIF) /* set when register is not empty */
continue;
return RCREG;
}
unsigned char
getche(void)
{
unsigned char c;
putch(c = getch());
return c;
}
void main(void){
TRISB = 0;
PORTB=0;
unsigned char input;
INTCON=0; // purpose of disabling the interrupts.
init_comms(); // set up the USART - settings defined in usart.h
while(1){
char rxbyte= getch();
switch(rxbyte)
{
case '1':
PORTB=0xFF;
putch("hhh");
__delay_ms(3000);
break;
case 'b':
PORTB=0x00;
putch("hhh");
__delay_ms(3000);
break;
default :
break;
}
}
}
char rxbyte;
#include <stdio.h>
#include <htc.h>
#include "usart.h"
#ifndef _XTAL_FREQ
// Unless specified elsewhere, 4MHz system frequency is assumed
#define _XTAL_FREQ 20000000
#endif
__CONFIG( HS & WDTDIS & PWRTDIS & BORDIS & LVPDIS & WRTEN & DEBUGDIS & UNPROTECT);
/* A simple demonstration of serial communications which
* incorporates the on-board hardware USART of the Microchip
* PIC16Fxxx series of devices. */
void putch(unsigned char byte)
{
/* output one byte */
while(!TXIF) /* set when register is empty */
continue;
TXREG = byte;
}
unsigned char
getch() {
/* retrieve one byte */
while(!RCIF) /* set when register is not empty */
continue;
return RCREG;
}
unsigned char
getche(void)
{
unsigned char c;
putch(c = getch());
return c;
}
void main(void){
TRISD=0;
PORTD=0;
unsigned char input;
INTCON=0; // purpose of disabling the interrupts.
init_comms(); // set up the USART - settings defined in usart.h
while(1){
char rxbyte;
rxbyte= getch();
switch(rxbyte)
{
case '1':
PORTD=0xFF;
putch("hhh");
__delay_ms(20);
break;
case 'b':
PORTD=0x00;
putch("hhh");
__delay_ms(20);
break;
default :
break;
}
}
}
#ifndef _SERIAL_H_
#define _SERIAL_H_
#define BAUD 9600
#define FOSC 20000000L
#define NINE 0 /* Use 9bit communication? FALSE=8bit */
#define DIVIDER ((int)(FOSC/(16UL * BAUD) -1))
#define HIGH_SPEED 1
#if NINE == 1
#define NINE_BITS 0x40
#else
#define NINE_BITS 0
#endif
#if HIGH_SPEED == 1
#define SPEED 0x4
#else
#define SPEED 0
#endif
#if defined(_16F87) || defined(_16F88)
#define RX_PIN TRISB2
#define TX_PIN TRISB5
#else
#define RX_PIN TRISC7
#define TX_PIN TRISC6
#endif
/* Serial initialization */
#define init_comms()\
RX_PIN = 1; \
TX_PIN = 1; \
SPBRG = DIVIDER; \
RCSTA = (NINE_BITS|0x90); \
TXSTA = (SPEED|NINE_BITS|0x20)
void putch(unsigned char);
unsigned char getch(void);
unsigned char getche(void);
#endif
TRY THIS CODE:
[CODE]
#include<pic.h>
#define _XTAL_FREQ 20e6
__CONFIG(0x3F3A);
void usrt_init()
{
TRISC6=0;
TXSTA=0b00100100; //CHECK THE DATA SHEET FOR TXSTA
RCSTA=0b10010000; //SEE THE DATA SHEET FOR RCSTA
BRGH=0; // low baud rate
SPBRG=129; //baud rate 2400
}
void interrupt_enable()
{
GIE=1;
PEIE=1;
RCIE=1;
}
void interrupt UART() //interrupt service routine
{
if(RCREG == '1'){PORTD=0xff;}
else if(RCREG == '2'){PORTD = 0x00;}
}
void txd(char write_data)
{
TXREG = write_data;
while(!TRMT);
}
main()
{
TRISD=0;
usrt_init();
interrupt_enable();
txd('H'); txd('E'); txd('L'); txd('L'); txd('O');
while(1); //WAITING FOR INTERRUPT (SEE RESULT ON PORT D)
}
#include<htc.h>
#define _XTAL_FREQ 20e6
__CONFIG(0x3F3A);
void usrt_init()
{
TRISC6=0;
TXSTA=0b00100100; //CHECK THE DATA SHEET FOR TXSTA
RCSTA=0b10010000; //SEE THE DATA SHEET FOR RCSTA
BRGH=0; // low baud rate
SPBRG=129; //baud rate 2400
}
void interrupt_enable()
{
GIE=1;
PEIE=1;
RCIE=1;
}
void interrupt UART() //interrupt service routine
{
switch(RCREG)
{
case '1':
PORTD=0xFF;
putch("hhh");
break;
case 'b':
PORTD=0x00;
putch("hhh");
break;
default :
break;
}
//if(RCREG == '1'){PORTD=0xff;}
//else if(RCREG == '2'){PORTD = 0x00;}
}
void txd(char write_data)
{
TXREG = write_data;
while(!TRMT);
}
main()
{
TRISD=0;
usrt_init();
interrupt_enable();
txd('H'); txd('E'); txd('L'); txd('L'); txd('O');
while(1); //WAITING FOR INTERRUPT (SEE RESULT ON PORT D)
}
#include<htc.h>
#define _XTAL_FREQ 20e6
__CONFIG(0x3F3A);
void usrt_init()
{
TRISC6=0;
TXSTA=0b00100100; //CHECK THE DATA SHEET FOR TXSTA
RCSTA=0b10010000; //SEE THE DATA SHEET FOR RCSTA
BRGH=0; // low baud rate
SPBRG=129; //baud rate 2400
}
void interrupt_enable()
{
GIE=1;
PEIE=1;
RCIE=1;
}
void txd(char write_data)
{
TXREG = write_data;
while(!TRMT);
}
void interrupt UART() //interrupt service routine
{
switch(RCREG)
{
case '1':
PORTD=0xFF;
txd('h');
break;
case 'b':
PORTD=0x00;
txd('E');
break;
default :
break;
}
//if(RCREG == '1'){PORTD=0xff;}
//else if(RCREG == '2'){PORTD = 0x00;}
}
main()
{
TRISD=0;
usrt_init();
interrupt_enable();
txd('H'); txd('E'); txd('L'); txd('L'); txd('O');
while(1); //WAITING FOR INTERRUPT (SEE RESULT ON PORT D)
}
char rxbyte;
unsigned char rxbyte;
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?