Showing posts with label PORTB. Show all posts
Showing posts with label PORTB. Show all posts

Friday, March 19, 2010

dsPIC30F3012 rs232 115200 bps

Now the program and circuit to connect a dsPIC 30F3012 via serial to a PC. The serial connection is made using a Bluetooth module fro kc wirefree (R). The KC21.
BLUETOOTH characteristics

Sunday, March 7, 2010

RS232 and the PIC18F2550

The next code connects a PIC to a computer via RS232.

//Program to receive characters over RS232, if the received character is "A" then set B0 Other character set B1 "X" low B0
#include <18F2550.h>//Selects the microcontroller
#fuses HS,NOWDT,NOPROTECT,PLL5,CPUDIV1//High freq clock, No WDT, No code protection, No low voltage programming
#use delay(clock=20000000)//Xtal frequency = 20MHz
#use rs232(baud=115200, xmit=PIN_C6, rcv=PIN_C7)//11.5kbps
void wait() //Subroutine to wait for 2ms or until a character is received
{
  int countdown;        //Define a variable to coutndown 
  countdown=200;//Charge value to 200 times 10us
  while((--countdown!=0)&&!kbhit()) //kbhit returns true when rs232 receives a character
    delay_us(10);//do a delay of 10us
}
void main() //Main program
{
  char pos;//Define the variable to receive the character
  set_tris_b(0);//Define PORTB as output
  pos='0';//Charge an initial value to the variable
  while(TRUE) //Do a loop
{
if((pos=='A'))//If the received character is an "A"
  output_high(PIN_B0);//Turn on the whole PORTB
  else if ((pos=='X'))
  output_low(PIN_B0);//Turn on the whole PORTB
else//otherwise
  output_high(PIN_B1);//Turn off the more significant bits
  wait();//Wait for visualization or another character
  if(kbhit()) //If a character is received
{
   pos=getc();//Save the received character in the 'pos' variable
   }
}
}

Basically the micro turns on a LED connected in the PORTB 0 (pin 21)when the character received is an “A” and turns off the same pin when the character received is “X”. In case of receive any other character turns the B1 (pin 22).

NOTE:

While doing this program I faced several problems.

My connection RS232 is via Bluetooth using a KC-21 module of KC- wirefree. The problem was that, once the connection Bluetooth was achieved, the PIC could send information but it was unable to receive it. It was necessary reinitialize the PIC in order to be able to receive data.

My original firmware had a delay just after configure the PORT B as output for allowing a stabilization of the RS232 (I thought) but when I eliminated this delay, everything worked fine.

image

The Bluetooth module connects to the PC when the PC tries to connect to the port mapped for that module, in this case COM44 for my module and PC. after the connection the PIC is able to receive and sent data smoothly!

Below the pic of my circuit and the terminal RS232 in the PC using the terminal of the mikro C compiler (Please, don’t be confused, the code is written in the CCS compiler)

I hope it works fine after tomorrow also. Now is time to leave the lab.

Tuesday, March 2, 2010

Using the ADC of the PIC18F2550

 

NOTE: The program was wrote using the CCS C compiler PIC C with a license PCWHD

The next program reads the 10-bit ADC of the PIC18F2550, channel 0(pin 2), and send the  higher 8 bits of the result through the port B

The sampling rate is  1kHz approximately.

  1: #include <18F2550.h>			//Selecting the microcontroller
  2: #fuses HS,NOWDT,NOPROTECT,NOLVP		//Selecting set-up (high freq oscillator, no watch dog timer, et)
  3: #use delay(clock=20000000)		//We gonna use a 20MHz crystal
  4: void main()				//Main function
  5:  {    unsigned int8  value;		//Defines a variable called "value", length 8 bits
  6:    setup_adc_ports(AN0);		//Defines wich port will be used for the ADC module, ANO (pin 2)
  7:    set_tris_b(0xFF);     		//Sets the whole port B as output 
  8:    setup_adc(ADC_CLOCK_DIV_32);         //Selects the TAD=1.6us (Xtal=20MHz) 
  9:    set_adc_channel(0);                  //Selects the channel from were the measures will be taken
 10:    do	 	             	//Initiates a loop
 11: 	{
 12: 		delay_ms(1);		//Delay of 1ms between measures
 13: 		value = read_adc();        //Starts the ADC module which return a value and this value is saved.
 14: 		output_b(value);    	//The ADC returned value is sent to the port B
 15: 	} 	
 16: 	while (TRUE);			//The loop never ends	
 17: }

Important issues:

The ADC module takes 11TAD to gat a 10-bit conversion. The TAD time is defined by the instruction

setup_adc(ADC_CLOCK_DIV_32);

In this case we divide the clock (20MHz) into 32 to get a time of 1.6us. This is the minimum time of adquisition for the ADC module.  As the module takes 11 TAD to performs the conversion in 10-bit, then the total time of conversion is:

Total conversion time=11*TAD=11*(1.6uS)=17.6uS

If we wish a more accurate sampling rate we should take this value into account and subtract it from our delay routine. In this case, for a 1kHz sampling rate we should put a delay of:

Delay=1ms-17.6uS=982.4uS

This value without take into account the time that each instruction take to be executed by the micro.

image

The image above shows the results of the program in a simulation made using the PIC18 simulator.

Note that for a 468 in decimal, the output in binary is 0111010100 That is the value showed by the LEDS without the two bits least significant.

image

The circuit in breadboard working.

References:

Related Posts Plugin for WordPress, Blogger...