PIC18F4550 applications & etc

Discussion about projects that used PIC Microcontroller, Hardware Interface, Programming Algorithm and etc......

PIC18F4550 applications & etc

Postby ken_alone » Sun Mar 10, 2013 4:01 am

Hi guys, quite sometime far apart from here.
I might want to ask about uart for pic. Is it a function that is consider built in pic or we need to have another device to like rs232 to enhance the data transmitting of the pic. I'm quite confusing so i might need some opinions to research for my hobby kits.^^

and for my second problem, recently i bout a joystick from cytron in order to use it as an analogue input in pic18f4550. Though, i can't make it work whatever i try. It just respond on a positive value rather than negative values. My codings are as below
CODE: SELECT_ALL_CODE
#include <18F4550.h> // PIC18F4550 HEADER FILE
#fuses XT,NOWDT,NOLVP,NOPROTECT // EXTERNAL CLOCK, NO WATCH DOG TIMER, NO LOW VOLTAGE
#device adc=10 // USE 10 BIT ADC QUANTIZATION
#use delay (clock=8M) // 4 MHZ CRYSTAL

#include <lcd.c>
/******************************************************************************/
#define Lcd_Rs          PIN_D1
#define Lcd_Rw          PIN_D2
#define Lcd_Data4       PIN_D4
#define Lcd_Data5       PIN_D5
#define Lcd_Data6       PIN_D6
#define Lcd_Data7       PIN_D7
void main()
{
   int32 range,x,y;
   lcd_init();
   setup_adc(ADC_CLOCK_INTERNAL); // set internaal clock for ADC
   setup_adc_ports(AN0_TO_AN2);    // set port A0 and A2 as analog input
   set_tris_b(0x00);
   set_tris_a(0xFF);
   output_b(0x00);
   
   while(true)
{         
       set_adc_channel(2);
       x=read_adc();
       x=(x*5*255/1023)-128;
       set_adc_channel(1);
       y=read_adc();
       y=(y*5*255/1023)+123;
       printf(lcd_putc,"\fx = %d degree %%\n y= %d degree",(int)x,(int)y);
       if((x>=0)&&(x<45))
      {
        output_high(PIN_B7);
      }
       if(x>=100)
      {
        output_high(PIN_B6); //led on
      }
       if(y>=100)
     {
        output_high(PIN_B1);
     }
       else if(y<28)
    {
         output_high(PIN_B0);
     }
      else
    {
      output_b(0x00);
    }
}
}

I programmed it with two types, AND function with upper values and lower values, another one with either greater or less than. But it doesn't seems works with pic. Only the positive value greater than programming works (x>=100) and (y>=100) and i wonder why??? or does the SWPSJOY need another circuit to support it's rotary functions??
all advice are greatly welcomed.. thank you...^^
ken_alone
Apprentice
 
Posts: 34
Joined: Thu Dec 31, 2009 3:40 pm

Re: PIC18F4550 applications & etc

Postby Brian Griffin » Mon Mar 11, 2013 10:19 am

Hello,

Be reminded that the ADC values are from 0-1023 in the majority of the PIC16/18F microcontrollers. There are no negative numbers whatsoever inside the ADC. The analogue joystick cannot give a value as it is only a potentiometer.

For example, if you want to go left by the joystick, you put a range picked up by the ADC, like 0 < x < 511, and if by the right, you will do 511 < x < 1023. Since the joystick is a potentiometer (analogue input), connect the joystick to a multimeter first, and measure the resistance when the thing goes left, and try the same when you push it to left. The higher resistance when you go a certain direction, the lower the ADC value will be obtained and vice versa. ;)
PIC - UIC00B from Cytron (replacement for my broken PICKit 2), Pickit 3, MikroC for PIC
dsPIC - MikroC for dsPIC, mikromedia board (dsPIC33)
AVR - AVR Dragon
Parallax - Prop tool
User avatar
Brian Griffin
Enthusiast
 
Posts: 403
Joined: Mon Jan 17, 2011 9:36 am

Re: PIC18F4550 applications & etc

Postby ken_alone » Mon Mar 11, 2013 1:49 pm

okok... i will try it at once... thanks for your advice yea...^^
ken_alone
Apprentice
 
Posts: 34
Joined: Thu Dec 31, 2009 3:40 pm

Re: PIC18F4550 applications & etc

Postby ken_alone » Sun Mar 31, 2013 11:06 pm

Hi brian, i do what u have mentioned about.
CODE: SELECT_ALL_CODE
 if((x>=0)&&(x<10))
        {
         output_high(PIN_B6);
        }
       else if((x>10)&&(x<=12))
       {
         output_high(PIN_B6); //led on
       }
       else if((y<3)&&(y>=0))
       {
         output_high(PIN_B1);
       }
       else if((y<=5)&&(y>=3))
       {
         output_high(PIN_B0);
       }
       else
       {
        output_b(0x00);
       }

My code was something like this. It works for value x more than 10 and less than 12, value y more than 3 and less than 5. the other x >0 and x<10 wasn't able to give me any results. i have tried x==0 but still not getting any progress. Do it related to any problem??
ken_alone
Apprentice
 
Posts: 34
Joined: Thu Dec 31, 2009 3:40 pm


Return to PIC Microcontroller

Who is online

Users browsing this forum: No registered users and 35 guests