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

help in pic16f506!!!

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







help in pic16f506!!!
PostPosted: Wed Jan 28, 2009 3:35 am     Reply with quote

I need help for my codes. I had written codes for my pic16f506 but it doesn't give any output. Please help and my codes in ccs compiler are as shown below. I still do not see any wrong with it. There were no errors when compiled.
Code:

#IF defined(__PCB__)
#include <16F506.h>
#device adc=8

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES HS                       //High speed Osc (> 4mhz)
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOMCLR                     //Master Clear pin disabled
#FUSES IOSC8                    //INTOSC speed 8 MHz
//#byte cm = 0x08
#use delay(clock=20000000)
//#use rs232(baud=9600,parity=N,xmit=PIN_B0,rcv=PIN_B0,bits=8)

            int16 ch[3];
            int i,m,n;
            void pread();         
            void pulse();           
            void pulser();
            void check();
            void main()
            {
                m=0;
                setup_comparator(NC_NC_NC_NC);
                //cm=cm&(0x00);
                set_tris_c(0);
                setup_adc_ports(AN0_AN1_AN2);
                setup_adc(ADC_CLOCK_DIV_32);
               // setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
                while (TRUE)
                  { 
                     //output_high(pin_c4);
                     pread();
                     check();
                     if(n==0)
                     {output_high(pin_c4);
                     //output_c(~0xFF);
                     pulse();
                     }
                     else if (n==1)
                     {output_high(pin_c5);
                    // output_c(~0xFE);
                     pulser();
                     }
                     else
                     //output_c(~0xFD);
                     pread();
                     }
                   }             
             void pread()
             {           
             for (i=0;i<3;i++)
                      {
                      set_adc_channel(i);
                      delay_us(100);
                      ch[i]=read_adc();
                      delay_us(100);
                         }
             }
             void pulse()
             {
             if (m==4)
             {
             m=0;
             output_c((0x01)<<m);
             }
             else
             {
             output_c(0x01<<m);
             }
             m++;
             delay_us(50);
             }
             void pulser()
             {
              if (m==0)
             {
             output_c((0x01)<<m);
             m=4;
             }
             else
             {
             output_c((0x01)<<m);
             }
             m--;
             delay_us(50);
             }
             void check()
             {
             if ((ch[0]>ch[1])&&(ch[0]>ch[2]))
             n=0;
             else if ((ch[2]>ch[1])&&(ch[2]>ch[0]))
             n=1;
             else if ((ch[1]<ch[2])&&(ch[1]<ch[0]))
             n=2;
             else
             n=3;
             }
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jan 28, 2009 1:58 pm     Reply with quote

Quote:
#FUSES NOWDT
#FUSES HS
#FUSES NOPROTECT
#FUSES NOMCLR
#FUSES IOSC8

Are you using an external 20 MHz crystal, or do you want to use the
internal RC oscillator ?

Also, have you ever made this PIC do anything ? For example, have
you made the PIC on this board blink an LED ? Were you successful ?
daraos



Joined: 16 Oct 2008
Posts: 18
Location: Chile

View user's profile Send private message

PostPosted: Wed Jan 28, 2009 2:07 pm     Reply with quote

shouldn't the functions be outside of Main?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jan 28, 2009 2:14 pm     Reply with quote

His formatting is poor, but the functions are below main().
kolo
Guest







PostPosted: Thu Jan 29, 2009 5:05 am     Reply with quote

I found out whats wrong already.
The setup_adc need ADC_CLOCK_INTERNAL to work. Thanks those who spend time reading my messy code. Btw, if I use HS 20 mhz then no need #fuses iosc? Please tell me what is the meaning of poor formatting? I need feedback for improving coding skills. Thanks!!!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jan 29, 2009 4:11 pm     Reply with quote

There are many different indent/brace position styles. This document
lists several of them:
http://en.wikipedia.org/wiki/Indent_style
The main thing wrong with your style is the massive tabbing on some
indents, and lack of indents in other spots.

Also it's easier to read code if the main() code is separated from
functions below it. This could be with some blank lines or a
separator line, such as:

Code:

//========================================

or

//----------------------------------------

It makes it clear where main() ends and the functions begin.
I use separator lines between every function, for clarity.
kolo
Guest







PostPosted: Thu Jan 29, 2009 7:37 pm     Reply with quote

Thx a lot. What about the #fuses iosc?? If i use #fuses HS for 20 MHz then do i stil need to include #fuses iosc?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jan 29, 2009 7:44 pm     Reply with quote

The PIC data sheet says this:
Quote:
REGISTER 10-2: CONFIG: CONFIGURATION WORD REGISTER
IOSCFS: Internal Oscillator Frequency Select bit
1 = 8 MHz INTOSC speed
0 = 4 MHz INTOSC speed

That bit controls the internal oscillator speed. Since you're using an
external crystal in HS mode, the internal oscillator is not used.
It doesn't matter what the speed setting is for it. So you shouldn't
need to specify the IOSCx fuse.
kolo
Guest







PostPosted: Thu Jan 29, 2009 8:50 pm     Reply with quote

Thanks a lot!!!
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