arson88
- 11th April 2011, 11:59
I am using SKKCA-21, a Bluetooth Module From Cytron. I have read a thread at
http://www.microchip.com/forums/m221431.aspx
I have a favour to ask, what is missing in my PIC Basic Pro Coding below? My question is how do I establish serial communication between a PC and a micro controller?
I am using PIC16F77A to connect to a PC via a bluetooth module but the hyperterminal does not displays anything. Basically, the coding should transfer ASCII character 0, 1, until 9 to the PC when a push button is pressed.
Disclaimer:
The assembly language and C language does not perform the same function as my coding.
Details:
The language used is PIC Basic Pro, Compiler: MicroCode Studio Version 4.0, Assembler: Microchip/mpasm suite, MPLAB IDE v8.66, Windows 7.
INCLUDE "modedefs.bas" ' Serial communication mode definition file
DEFINE OSC 20 ' Oscillator speed is set to 20MHz
TRISB = %11111111
TRISC = %10000000 ' RC.7 => USART RX pin set to input
PortC = 0
SPBRG = 10 ' Baud Rate = 115200
' Variable definition
' ===================
'
count01 VAR BYTE ' variable to store TXREG content
TXSTA = %00100100 ' Enable transmit and asynchronous mode
RCSTA = %10010000 ' Enable serial port and continuous receive
mainloop:
IF (PortB.7 = 1) THEN
FOR count01 = 0 TO 9
PAUSE 2000
TXREG = count01 + $30 ' Transmit Data
HIGH PortD.2
PAUSE 2000
LOW PortD.2
Next
ENDIF
GOTO mainloop
END
This is the coding from the microchip forum:
LIST p=16F628a
#include <P16F628a.inc>
__config 0x2118
X1 equ 0x30
X2 equ 0x31
X3 equ 0x32
org 0x0000
start
goto initialize
org h'0004' ;interrupt vector location
retfie
;************************
;INITIALIZATION ROUTINE *
;************************
initialize
;initialize ports and registers
movlw 0x07
movwf CMCON ;turn comparators off
bcf STATUS,RP0
gie01 bcf INTCON,GIE ;turn gie off
btfsc INTCON,GIE
goto gie01
clrf PIR1 ;clear peripheral flags
clrf PORTA ;clear all i/o registers...
clrf PORTB
bsf STATUS,RP0
movlw b'00011111'
movwf TRISA
movlw b'00000110' ; RB1 = RX input, RB2=TX output
movwf TRISB
clrf INTCON
clrf PIE1 ; no interrupt
;uart specific initialization
;txsta=Transmit STAtus and control register.
bcf TXSTA,TX9 ; <6> 0 select 8 bit mode
bsf TXSTA,TXEN ; <5> 1 enable transmit function
bcf TXSTA,SYNC ; <4> 0 asynchronous mode.
bsf TXSTA,BRGH ; <2> 0 disable high baud rate generator !!!
xtal_freq = d'4000000' ;crystal frequency in Hertz.
baudrate = d'115200' ;desired baudrate.
spbrg_value = (xtal_freq/(baudrate*d'16'))-1
movlw spbrg_value ;set baud rate generator value
movwf SPBRG
bcf STATUS,RP0
;more uart specific initialization
;rcsta=ReCeive STAtus and control register
bsf RCSTA,SPEN ; 7 spen 1=rx/tx set for serial uart mode
bcf RCSTA,RX9 ; 6 rc8/9 0=8 bit mode
bcf RCSTA,SREN ; 5 sren 0=don't care in uart mode
bsf RCSTA,CREN ; 4 cren 1=enable constant reception
bcf RCSTA,FERR ; 2 ferr input framing error bit. 1=error
bcf RCSTA,RX9D ; 0 rx9d input (9th data bit). ignore.
movf RCREG,w ;clear uart receiver
movf RCREG,w
movf RCREG,w
movlw 0
movwf TXREG ;send out dummy character
; to get transmit flag valid!
;************************
; main programme *
;************************
main
loop
btfss PORTA, 2 ;test if input device is ringing
call ON1
goto loop
ON1
movlw 'A'
call transmitw ;send W to the UART transmitter
bsf PORTB, 6 ;turn on RB2
call Delay ;this waits for a while!
bcf PORTB, 6 ;turn off RB2
call Delay
goto loop
;*************************************************
;* TRANSMIT Routines *
;*************************************************
transmitw
btfss PIR1,TXIF
goto transmitw ;wait for transmitter interrupt flag
movwf TXREG ;load data to be sent...
return
;transmitted data is in W
;********************
;* Delay Routines *
;********************
Delay movlw d'2'
movwf X3
B3 movlw d'255'
movwf X2
B2 movlw d'255'
movwf X1
B1 decfsz X1,f
goto B1
decfsz X2,f
goto B2
decfsz X3,f
goto B3
return
end
Anyone have materials for reference ? I am not proficient in assembly language.
BTW, Cytron provided this coding in C for reference, a DIY project, PR6:
C language:
#include <pic.h>
__CONFIG(0x3F32);
#define seg PORTD // define 7 segment as PORTD
unsigned char a;
void init(void) // subroutine to initialize
{
// SPBRG=0x0A; // set baud rate as 115200 baud
SPBRG=0x81; // set baud rate as 9600 baud
BRGH=1;
TXEN=1;
CREN=1;
SPEN=1;
TRISD = 0b00000000;
seg = 0b00000000;
}
void display(unsigned char c) // subrountine to display the text on the screen
{
while (TXIF == 0);
TXREG = c;
}
unsigned char receive(void) // subrountine to receive text from PC
{
while (RCIF == 0);
a = RCREG;
return a;
}
void main(void)
{
init();
while(1) // wait for 'ok' to be entered
{
a = receive();
if (a == 'o')
{
a = receive();
if (a == 'k') break;
}
}
display('C'); // change the text for different display
display('y');
display('t');
display('r');
display('o');
display('n');
display(0x0a);
display(0x0d);
display('P');
display('r');
display('e');
display('s');
display('s');
display(0x20);
display('a');
display('n');
display('y');
display(0x20);
display('n');
display('u');
display('m');
display('b');
display('e');
display('r');
seg = 1;
while(1) // wait for number and display it on 7 segment
{
a = receive();
if (a=='1'||a=='2'||a=='3'||a=='4'||a=='5'||a=='6'||a =='7'||a=='8'||a=='9'||a=='0')
{
seg = a-0x30;
}
}
}
http://www.microchip.com/forums/m221431.aspx
I have a favour to ask, what is missing in my PIC Basic Pro Coding below? My question is how do I establish serial communication between a PC and a micro controller?
I am using PIC16F77A to connect to a PC via a bluetooth module but the hyperterminal does not displays anything. Basically, the coding should transfer ASCII character 0, 1, until 9 to the PC when a push button is pressed.
Disclaimer:
The assembly language and C language does not perform the same function as my coding.
Details:
The language used is PIC Basic Pro, Compiler: MicroCode Studio Version 4.0, Assembler: Microchip/mpasm suite, MPLAB IDE v8.66, Windows 7.
INCLUDE "modedefs.bas" ' Serial communication mode definition file
DEFINE OSC 20 ' Oscillator speed is set to 20MHz
TRISB = %11111111
TRISC = %10000000 ' RC.7 => USART RX pin set to input
PortC = 0
SPBRG = 10 ' Baud Rate = 115200
' Variable definition
' ===================
'
count01 VAR BYTE ' variable to store TXREG content
TXSTA = %00100100 ' Enable transmit and asynchronous mode
RCSTA = %10010000 ' Enable serial port and continuous receive
mainloop:
IF (PortB.7 = 1) THEN
FOR count01 = 0 TO 9
PAUSE 2000
TXREG = count01 + $30 ' Transmit Data
HIGH PortD.2
PAUSE 2000
LOW PortD.2
Next
ENDIF
GOTO mainloop
END
This is the coding from the microchip forum:
LIST p=16F628a
#include <P16F628a.inc>
__config 0x2118
X1 equ 0x30
X2 equ 0x31
X3 equ 0x32
org 0x0000
start
goto initialize
org h'0004' ;interrupt vector location
retfie
;************************
;INITIALIZATION ROUTINE *
;************************
initialize
;initialize ports and registers
movlw 0x07
movwf CMCON ;turn comparators off
bcf STATUS,RP0
gie01 bcf INTCON,GIE ;turn gie off
btfsc INTCON,GIE
goto gie01
clrf PIR1 ;clear peripheral flags
clrf PORTA ;clear all i/o registers...
clrf PORTB
bsf STATUS,RP0
movlw b'00011111'
movwf TRISA
movlw b'00000110' ; RB1 = RX input, RB2=TX output
movwf TRISB
clrf INTCON
clrf PIE1 ; no interrupt
;uart specific initialization
;txsta=Transmit STAtus and control register.
bcf TXSTA,TX9 ; <6> 0 select 8 bit mode
bsf TXSTA,TXEN ; <5> 1 enable transmit function
bcf TXSTA,SYNC ; <4> 0 asynchronous mode.
bsf TXSTA,BRGH ; <2> 0 disable high baud rate generator !!!
xtal_freq = d'4000000' ;crystal frequency in Hertz.
baudrate = d'115200' ;desired baudrate.
spbrg_value = (xtal_freq/(baudrate*d'16'))-1
movlw spbrg_value ;set baud rate generator value
movwf SPBRG
bcf STATUS,RP0
;more uart specific initialization
;rcsta=ReCeive STAtus and control register
bsf RCSTA,SPEN ; 7 spen 1=rx/tx set for serial uart mode
bcf RCSTA,RX9 ; 6 rc8/9 0=8 bit mode
bcf RCSTA,SREN ; 5 sren 0=don't care in uart mode
bsf RCSTA,CREN ; 4 cren 1=enable constant reception
bcf RCSTA,FERR ; 2 ferr input framing error bit. 1=error
bcf RCSTA,RX9D ; 0 rx9d input (9th data bit). ignore.
movf RCREG,w ;clear uart receiver
movf RCREG,w
movf RCREG,w
movlw 0
movwf TXREG ;send out dummy character
; to get transmit flag valid!
;************************
; main programme *
;************************
main
loop
btfss PORTA, 2 ;test if input device is ringing
call ON1
goto loop
ON1
movlw 'A'
call transmitw ;send W to the UART transmitter
bsf PORTB, 6 ;turn on RB2
call Delay ;this waits for a while!
bcf PORTB, 6 ;turn off RB2
call Delay
goto loop
;*************************************************
;* TRANSMIT Routines *
;*************************************************
transmitw
btfss PIR1,TXIF
goto transmitw ;wait for transmitter interrupt flag
movwf TXREG ;load data to be sent...
return
;transmitted data is in W
;********************
;* Delay Routines *
;********************
Delay movlw d'2'
movwf X3
B3 movlw d'255'
movwf X2
B2 movlw d'255'
movwf X1
B1 decfsz X1,f
goto B1
decfsz X2,f
goto B2
decfsz X3,f
goto B3
return
end
Anyone have materials for reference ? I am not proficient in assembly language.
BTW, Cytron provided this coding in C for reference, a DIY project, PR6:
C language:
#include <pic.h>
__CONFIG(0x3F32);
#define seg PORTD // define 7 segment as PORTD
unsigned char a;
void init(void) // subroutine to initialize
{
// SPBRG=0x0A; // set baud rate as 115200 baud
SPBRG=0x81; // set baud rate as 9600 baud
BRGH=1;
TXEN=1;
CREN=1;
SPEN=1;
TRISD = 0b00000000;
seg = 0b00000000;
}
void display(unsigned char c) // subrountine to display the text on the screen
{
while (TXIF == 0);
TXREG = c;
}
unsigned char receive(void) // subrountine to receive text from PC
{
while (RCIF == 0);
a = RCREG;
return a;
}
void main(void)
{
init();
while(1) // wait for 'ok' to be entered
{
a = receive();
if (a == 'o')
{
a = receive();
if (a == 'k') break;
}
}
display('C'); // change the text for different display
display('y');
display('t');
display('r');
display('o');
display('n');
display(0x0a);
display(0x0d);
display('P');
display('r');
display('e');
display('s');
display('s');
display(0x20);
display('a');
display('n');
display('y');
display(0x20);
display('n');
display('u');
display('m');
display('b');
display('e');
display('r');
seg = 1;
while(1) // wait for number and display it on 7 segment
{
a = receive();
if (a=='1'||a=='2'||a=='3'||a=='4'||a=='5'||a=='6'||a =='7'||a=='8'||a=='9'||a=='0')
{
seg = a-0x30;
}
}
}