tonyyy
Member level 1
- Joined
- Dec 26, 2012
- Messages
- 38
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,286
- Activity points
- 1,663
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 #include <stdio.h> #include <htc.h> #include "usart.h" #include <stdlib.h> #ifndef _XTAL_FREQ #define _XTAL_FREQ 10000000 #endif void putch(unsigned char byte) { while(!TXIF) //wait until TXIF is high continue; TXREG = byte; //put byte into Transmit Register TXIF = 0; } unsigned char getch() {/* retrieve one byte */ while(!RCIF) /* set when register is not empty */ continue; RCIF = 0; return RCREG; } unsigned char getche(void) { unsigned char c; putch(c = getch()); return c; } void init_enable() { BRGH = 1; /* high baud rate */ SYNC = 0; /* asynchronous */ SPEN = 1; /* enable serial port pins */ CREN = 1; /* enable reception */ SREN = 0; /* no effect */ TXIE = 0; /* disable tx interrupts */ RCIE = 0; /* disable rx interrupts */ TX9 = 0; /* 8- or 9-bit transmission */ RX9 = 0; /* 8- or 9-bit reception */ TXEN = 1; /* enable the transmitter */ RCIE =1; PEIE =1; } void main(void){ //printf("TEST - 12/12/2013", '\n'); char rxbyte; TRISB = 0; PORTB = 0; INTCON=0; INIT_COMMS(); // set up the USART - settings defined in usart.h init_enable(); // just blinking a led int a; a = 0; while(a < 5) { PORTB=0xFF; __delay_ms(300); PORTB=0x00; __delay_ms(300); a = a +1 ; } printf("********* WELCOME ***********", '\n'); while(1){ rxbyte= getche(); unsigned char input; switch(rxbyte) { case '1': { PORTB=0xFF; printf("led on", '\n'); putch('\n'); } break; case '2': { PORTB=0x00; printf("led off", '\n'); putch('\n'); } break; case '4': { int i; for(i=0;i!=100; i++) { putch('a'); <- when i send 4 i don't get it back. But with PRoteus and PIMCOM works! } } default : break; } } }
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 #ifndef _SERIAL_H_ #define _SERIAL_H_ #define BAUD 9600 #define FOSC 10000000L #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 #define RX_PIN TRISC7 #define TX_PIN TRISC6 /* 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
You have to use Serial interrupt for data receive. Post your Projects files and Proteus file in a zip file.
#include <stdio.h>
#include <htc.h>
#include <stdlib.h>
#ifndef _XTAL_FREQ
#define _XTAL_FREQ 10000000
#endif
void main(void){
TRISB = 0;
PORTB = 0;
int a;
a = 0;
while(a < 25) {
PORTB=0xFF;
__delay_ms(300);
PORTB=0x00;
__delay_ms(300);
a = a +1 ;
}
PORTB=0xFF;
}
#include <stdio.h>
#include <htc.h>
#include "usart.h"
#include <stdlib.h>
/* A simple demonstration of serial communications which
* incorporates the on-board hardware USART of the Microchip
* PIC16Fxxx series of devices. */
#ifndef _XTAL_FREQ
#define _XTAL_FREQ 10000000
#endif
// bit di configurazione
__CONFIG(CP_OFF & BOREN_OFF & LVP_OFF & WDTE_OFF & PWRTE_ON & FOSC_HS );
void putch(unsigned char byte)
{
while(!TXIF) //wait until TXIF is high
continue;
TXREG = byte; //put byte into Transmit Registe
}
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 init_enable()
{
BRGH = 1; /* high baud rate */
SYNC = 0; /* asynchronous */
SPEN = 1; /* enable serial port pins */
CREN = 1; /* enable reception */
SREN = 0; /* no effect */
TXIE = 0; /* disable tx interrupts */
RCIE = 1; /* disable rx interrupts */
TX9 = 0; /* 8- or 9-bit transmission */
RX9 = 0; /* 8- or 9-bit reception */
TXEN = 1; /* enable the transmitter */
PEIE =1;
}
void main(void){
char rxbyte;
TRISB = 0;
PORTB = 0;
INTCON=0;
INIT_COMMS(); // set up the USART - settings defined in usart.h
init_enable();
putch('a');
putch('b');
putch('c');
printf("before while ", '\n');
while(1){
rxbyte = getch();
putch('a');
putch('b');
putch('c');
printf(" i am in while ", '\n');
switch(rxbyte)
{
case '1':
{
PORTB=0xFF;
printf("led on", '\n');
putch('\n');
}
break;
case '2':
{
PORTB=0x00;
printf("led off", '\n');
putch('\n');
}
break;
case '3':
{
int a;
a = 0;
printf("blinking", '\n');
while(a < 5) {
PORTB=0xFF;
__delay_ms(300);
__delay_ms(300);
a = a +1 ;
}
}
break;
case '4':
{
int i;
for(i=0;i!=100; i++)
{
putch('a');
}
}
case '5':
{
}
default :
break;
}
}
}
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?