CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

any comment?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
neal1
Guest







any comment?
PostPosted: Thu Feb 26, 2009 7:51 am     Reply with quote

I just wrote some code for serial communication with my PIC16f628a.

I just need some comment on my code...please comments.
I compiled these code with MPLAB v8.10 and no error occur.
Any comments are welcome:P
code as below:
Code:
#include <ctype.h>
#include <16f628a.h>
#FUSES NOWDT, HS, NOPUT, NOPROTECT, BROWNOUT, MCLR, NOLVP, NOCPD
#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_B2,rcv=PIN_B1,bits=8)
//#byte CMCON  =0x1f 
#byte    CMCON = 0x1f
#byte   portb = 0x06
#byte    porta = 0x05
#byte    RCSTA = 0x18
#byte    Txreg =   0x19
#byte    Rxreg =   0x1A

#bit     RCIF  = 0x0c.5   // PIR1 register,Flag bit RCIF will be set when reception is complete
#bit   TXIE  = 0x8c.4  //PIE register Flag bit TXIE is zero for Disables the USART transmit interrupt

#byte   TXSTA = 0x98
#bit   BRGH = TXSTA.2
#bit   SYNC = TXSTA.4
#bit   TXEN = TXSTA.5
#bit   TX9  = TXSTA.6

#byte    RCSTA= 0x18
#bit   RX9  = RCSTA.6
#bit   SPEN = RCSTA.7
#bit   CREN = RCSTA.4
      

#byte SPBRG=0x99
void main()
{

   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   setup_oscillator(False);

   // TODO: USER CODE!!
output_low(portb);
output_low(porta);
CMCON =0x07;         //enable pins for I/O function
SET_TRIS_A(0x00);      //TRISA<5> always read as 1
SET_TRIS_b(0x02);      //TRISB<1> bit needs to be set and TRISB<2> bit cleared in order to configure pins RB2/TX/CK and RB1/RX/DT as the Universal Synchronous Asynchronous Receiver Transmitter pins.
SPBRG=0x81;               //129 decimal, using ASYNCHRONOUS MODE (BRGH = 1)
output_low(SYNC);      //Asynchronous mode
output_high(TXEN);      //Enable transmission
output_low(TX9);      //enable 8bit transmittion
output_high(BRGH);      //Asynchronous mode high speed
output_high(SPEN);      //enable serial port receive
output_low(RX9);      //enable 8 bit reception
output_high(CREN);      //Enables continuous receive

output_low(portb);
output_low(porta);


while(1){
printf("srsrsrsr\n");


if (RCIF==1){
porta ='A';

}//end if


   }//end while
}//end main

Wink Wink Wink
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Feb 26, 2009 12:36 pm     Reply with quote

Comments:

1. Don't do direct register access. You don't need to do it.
Invoke the CCS library code with a #use statement, and
use CCS functions.

2. Before using CCS functions, read the manual on each function
and see how to properly use it.
http://www.ccsinfo.com/downloads/ccs_c_manual.pdf

3. Don't just "throw" code together. Understand every line, and
use the proper parameters for a function. In embedded
programming, everything is important.
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Thu Feb 26, 2009 1:20 pm     Reply with quote

Furthermore, if accessing SFR directly for some reason, do it according to the CCS C documentation. output_low() and output_high() are creating special code for IO ports, including direction register setup. They aren't intended for any other SFRs and most likely cause unexpected behaviour. They surely don't work with a byte address, as in your code.
neal1
Guest







PostPosted: Fri Feb 27, 2009 6:19 am     Reply with quote

How about writing like this way? I simulate with MPSIM and seem work fine...
Code:

#include <ctype.h>
#include <16f628a.h>
#FUSES NOWDT, HS, NOPUT, NOPROTECT, BROWNOUT, MCLR, NOLVP, NOCPD
#use fast_io(a)
#use fast_io(b)
#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_B2,rcv=PIN_B1,bits=8,ENABLE=PIN_B4)
int i;
void main()
{

   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   setup_oscillator(False);

   // TODO: USER CODE!!
output_a(0x00);
output_b(0x00);
SET_TRIS_A(0x00);      //TRISA<5> always read as 1
SET_TRIS_b(0x02);      //TRISB<1> bit needs to be set and TRISB<2> bit cleared in order to configure pins RB2/TX/CK and RB1/RX/DT as the Universal Synchronous Asynchronous Receiver Transmitter pins.

i=getc();
while(1){
printf("welcome\n");
printf("press 1 to Turn On LED1\n" );
printf("press 2 to Turn On LED2\n" );


switch(i){

case'1':
printf("LED1 Turned ON");
break;

case'2':
printf("LED2 Turned ON");
break;


/*if (kbhit())//wait until a character has been received
printf("hello!!\n");
else if (PIN_B3==1)
printf("hi!!\n");*/


   }
   }//end while
}//end main
Question Question Question BUT I NOT SURE CAN SWITCH THE 'i' CORRECTLY?
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Sun Mar 01, 2009 1:38 pm     Reply with quote

Code:
#use rs232(baud=9600,parity=N,xmit=PIN_B2,rcv=PIN_B1,bits=8,ENABLE=PIN_B4)
Do you really require the ENABLE keyword? This is normally only used for switching the external bus drivers to a tri-state mode. Not a common thing to do in RS232, unless you are using another driver like RS485 of course. Skipping the enable function will free a hardware pin for other usage.

An improvement would be to add the ERRORS keyword. Without this keyword the hardware UART will freeze on a receive buffer overflow (3 characters).

Code:
i=getc();
Move this line inside the loop. For example, just before the switch statement.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group