Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

C++ windows baud rate for PIC16F628A?

Status
Not open for further replies.

vlad777

Member
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:

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:
Do you poses a scope? See what the pic s actually running at.... I know its all set up correctly... The 4mhz internal clock cannot possibly be that far out ( its a precision clock).. I can't see the PC has the issue looking at the code, the only indication you are getting is Error or no error... I take it you get RB6 and RB4 on when its running? (X = 0x58) portb 4-7 = 5.

You need to see this on a scope..
 
I don't have a scope.

When I am out of the range I mentioned, I get errors (RA1,RA0).

When inside the range, RB4-7 counts normally. (send+=0x10; )


Is it possible that Microsoft has different definition of a baud?
 
Last edited:
Try sending characters with hyperterminal or some other terminal software.

Mike.
 
If you send a 0x00 byte, it makes ONE pulse, which is exactly 9bits in overall period (one 0 start bit and eight 0 data bits).

Then you can look on the 'scope at the pulse period and calculate the actual baud period as pulse period / 9. It's a handy trick.
 
I think higher case V in ascii resembles a square wave, sending a string of these would give an approx idea with a frequency counter, I've even done it in the past with a speaker connected in series with a 100r resistor to the data line, 9600 is a high pitched squeal.
Very heath robbo idea I know, but when you havent got anything you have to resort to these tricks.
 
Last edited:
I'd try hyperterminal (or equiv) first to find out which end is going wrong. Having untested software at both ends makes it extremely difficult to debug.

Mike.
 
I tried hyperterminal and everything works ONLY IF I set SPBRG to 18.
(18 is 25*8/11)

In hyperterminal settings says bits per seconds NOT bouds.

It looks to me that windows only counts in data bits(8), but microchip counts whole frame(10-12bits) for boud rate?
 
Doesn't matter how many bits in the transmission... The rate is still bits per second.... Baud = bps, end of...

If a pc talks to another at the correct baud... Then there is something amiss with the pic... If the PC is set to 9600 and your pic is set to 6750... Your pic is running a lot faster than 4mhz

If you need to set a spbrg of 38 ( 6750 baud) to achieve 9600... your pic is running at 5.8mhz
 
Last edited:
How can I force hyperterminal to send extended ASCII 240 ≡ character, so I can measure frequency (like dr pepper said in post #11).
I should measure about 1kHz, is that right?

If I have to use character V which is binary 0011010101 (start bit is leftmost, stop bit is rightmost) what frequency should I measure ?
 
@ Ian Rogers

My configuration word is:

0x3D18 or b'0011110100011000'

which should be:
100 = INTOSC oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN

About sending a file, there are some modem types (protocol) hyperterminal offers to select?

EDIT:

These protocols seem to require some kind of handshake (which I can not support).
 
Last edited:
PC C++ settings:
dcb.BaudRate=CBR_9600;

PIC settings:
__config 0x3D18
;-----
bsf STATUS,RP0
movlw 18 ;eighteen
movwf SPBRG
movlw b'00000100' ;SYNC-0 BRGH-1
movwf TXSTA
bcf STATUS,RP0

Frequencies for chars (PC generated):
char 'V' 0x56 -- 3.8KHz
char '≡' 0xF0 -- 0.9KHz

I don't have better resolution on my DMM.

Whit this settings everything works but SPBRG is set to 18 which should be faster than 9600 baud?

EDIT:
char V has 4 transitions (0 to 1) then 3.8khz/4 =9.5 khz which should be about 9600 baud so PC is OK.
So what is wrong with the pic?
 
Last edited:
Are you sure your default radix is decimal? I suspect it may be hexadecimal (18 Hex = 24 decimal) Try movlw .25 or movlw d'25' to force decimal.

Edit, MPASM defaults to hex and so I think this is your problem.

Mike.
 
Last edited:
Status
Not open for further replies.

Latest threads

Back
Top