C++ windows baud rate for PIC16F628A?
Here is the schematic:
**broken link removed**
A are errors, B are high nibble of received byte.
Here is pic code:
Here is windows C++ code:
I setup pic16f628a like this:
SPBRG=25
SYNC=0
BRGH=1
which should be 9600 baud.
But in windows when I set dcb.BaudRate to 9600 it doesn't work.
It does work if I set dcb.BaudRate between 6064 and 6776 baud;
My only clue is that 8/12*9600 is 6400.
(12 being max possible number of frame bits.)
Can someone tell me what is wrong here?
PS: I am only doing simplex for first experiment.
Many thanks.
Here is the schematic:
**broken link removed**
A are errors, B are high nibble of received byte.
Here is pic code:
Code:
errorlevel -302
LIST p=16F628 ;tell assembler what chip we are using
include "P16F628.inc" ;include the defaults for the chip
__config 0x3D18 ;sets the configuration settings (oscillator type etc.)
cblock 0x20 ;start of general purpose registers
var1
dy1
dy2
value
endc
reset org 0x00 ;reset vector address
goto main
nop
nop
nop
org 0x04 ;interrupts go to this location
goto irrpt
main
movlw 0x07
movwf CMCON ;turn comparators off (make it like a 16F84)
bsf STATUS,RP0
movlw b'00000110'
movwf TRISB
movlw b'00110000'
movwf TRISA
bcf STATUS,RP0
movlw 0x00
movwf CCP1CON
;----------------setup
bsf STATUS,RP0
movlw 25 ;18-win9600 ;25-pic9600
movwf SPBRG
movlw b'00000100' ;SYNC-0 BRGH-1
movwf TXSTA
bcf STATUS,RP0
bsf RCSTA,SPEN
bsf STATUS,RP0
bsf PIE1,RCIE
bcf STATUS,RP0
movlw 0xC0
movwf INTCON ;enable global and peripherial interrupt
bsf RCSTA,CREN
;---------------end_setup
movlw 0x00
movwf PORTA
movwf PORTB
movwf var1
loop
goto loop
;------------------main end
irrpt
movf RCREG,0
movwf value ;read received byte
movlw 0
movwf PORTA
btfsc RCSTA,OERR
goto goerr
btfsc RCSTA,FERR
goto gferr
movf value,0
movwf PORTB ;move to LEDs only if no error
retfie
goerr
bsf PORTA,0 ;set overflow LED
bcf RCSTA,CREN
bsf RCSTA,CREN
retfie
gferr
bsf PORTA,1 ;set frame error LED
retfie
end
Here is windows C++ code:
Code:
#include<stdio.h>
#include<conio.h>
#include<Windows.h>
int main(int argc,char* argv[]){
unsigned char send=0x00;
DWORD wttn=0;
HANDLE file=NULL;
COMMCONFIG cc;
DCB dcb;
int r=0;
file=CreateFile(L"COM1", GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, NULL);
r=GetCommState(file,&dcb);
dcb.DCBlength = sizeof(dcb);
dcb.BaudRate=6420; // 6064-6776 -works only in this range
dcb.ByteSize=8;
dcb.fParity=0;
dcb.fBinary=1;
dcb.Parity=NOPARITY;//SPACEPARITY;//NOPARITY;//
dcb.StopBits=ONESTOPBIT;//TWOSTOPBITS;//
dcb.fRtsControl=RTS_CONTROL_DISABLE;
dcb.fDtrControl=DTR_CONTROL_DISABLE;
dcb.EofChar=0x0;
dcb.fDsrSensitivity=0;
dcb.fInX=0;
dcb.fOutX=0;
SetCommState(file,&dcb);
char ch=0;
while(ch!=27){
send+=0x10;
Sleep(500);
wttn=1;
WriteFile(file,&send,1,&wttn,NULL);
//
printf("\n %X",send);
if(_kbhit())ch=_getch();
}
CloseHandle(file);
return 0;
}
I setup pic16f628a like this:
SPBRG=25
SYNC=0
BRGH=1
which should be 9600 baud.
But in windows when I set dcb.BaudRate to 9600 it doesn't work.
It does work if I set dcb.BaudRate between 6064 and 6776 baud;
My only clue is that 8/12*9600 is 6400.
(12 being max possible number of frame bits.)
Can someone tell me what is wrong here?
PS: I am only doing simplex for first experiment.
Many thanks.
Last edited: