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

PIC16F18326 internal OSC

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



Joined: 09 Oct 2009
Posts: 26

View user's profile Send private message Send e-mail

PIC16F18326 internal OSC
PostPosted: Wed May 03, 2023 10:19 am     Reply with quote

Hello,

I'm using PIC16F18326.
I need to read some ADC (AN0) inputs and according to the input value I have a green (PIN_A1) or red (PIN_A5) signal output.
I think I have some problem with Osc or clock frequency because my delays are wrong.
Other thing is: I only need to make one test (one program main), but it was always running as I have a while(true).
May I have some help from you?
Code:

#include <16F18326.h>
#device ADC=10
#FUSES LVP      // Should be NOLVP
#FUSES WDT                      //Watch Dog Timer
#FUSES RSTOSC_LFINTRC
#FUSES NOCLKOUT
#FUSES BROWNOUT, LPBOR, PUT, BORV27     // Power up Timer and Brownouts
#FUSES NOPROTECT  // Should be NOPROTECT
#fuses NOEXTOSC   // *** ADD THIS LINE RIGHT HERE ***
#use delay(internal=32000000) //,restart_wdt)

#BYTE OSCCON1 = 0x919  // OSCCON register (pg 47 datasheet)
#BYTE OSCCON2 = 0x91A  // OSCCON register (pg 47 datasheet)
#BYTE WDTCON = 0x97 // WDTCON (pg 33 datasheet)
//#use standard_io (ALL)

#include <stdio.h>
#include <stdlib.h>

// LEDs intensity
float LDR()
{
    int8 i;
    float intlum;
    float intlum_sum;
    float intlum_average;

    intlum = 0;   
    intlum_sum = 0;
    intlum_average = 0;
    i = 0;

    set_adc_channel(0);
    delay_us(5);

    for(i=0;i<100;i++)
    {
       intlum = read_adc();
       delay_us(5);
       intlum_sum += intlum;
       delay_us(5);
    }

    intlum_average = intlum_sum/100;

    return intlum_average;
}

void presenca()
{
   restart_wdt();
   float Position;
   Position = 0;
           
        // POSITION
    output_high(PIN_C0); // Position ON
    delay_ms(1000);
    Position = LDR();

   if(Position <= 70) // && Position >= 20)  // Position: 0.14 ~ 0.34 V (Read 0.242V)
   {
      //output_high(PIN_A1); // ON green
      //delay_ms(100);
      //output_low(PIN_A1);
      output_low(PIN_C0);
      //delay_ms(100);
   }
   else if(Position > 70) // || Position < 20)
   {
      output_high(PIN_A5); // ON Red
      delay_ms(1000);
      delay_ms(1000);
      delay_ms(1000);
      delay_ms(1000);
      delay_ms(1000);
      break;
   }
}

void STOP()
{
   restart_wdt();
   float Stop;
   Stop = 0;
   
   output_high(PIN_C1); // Stop ON
   delay_ms(1000);
   Stop = LDR();
     
   if(Stop <= 165) // || Stop >= 110)
   {
      //output_high(PIN_A1); // green
      //delay_ms(100);
      //output_low(PIN_A1);
      output_low(PIN_C1);
      //delay_ms(100);
   }
   else if(Stop > 165) // || Stop < 110)
   {
      output_high(PIN_A5); // ON red
      delay_ms(1000);
      delay_ms(1000);
      delay_ms(1000);
      delay_ms(1000);
      delay_ms(1000);
      break;
   }
}

void DI()
{
   restart_wdt();
   float Pisca;
   Pisca = 0;
   
   output_high(PIN_C2); // DI ON
   delay_ms(1000);
   Pisca = LDR();
   
   if(Pisca <= 155)
   {
      //output_high(PIN_A1); // green
      //delay_ms(100);
      //output_low(PIN_A1);
      output_low(PIN_C2);
      //delay_ms(100);
   }           
   else if(Pisca > 155)
   {
      output_high(PIN_A5); // ON red
      delay_ms(1000);
      delay_ms(1000);
      delay_ms(1000);
      delay_ms(1000);
      delay_ms(1000);
      break;
   }
}

void Fog()
{
   restart_wdt();
   float Nev;
   Nev = 0;
   
   output_high(PIN_C3); // fog ON
   delay_ms(1000);
   Nev = LDR();
   
   if(Nev <= 205)
   {
      //output_high(PIN_A1); // green
      //delay_ms(100);
      //output_low(PIN_A1);
      output_low(PIN_C3);
      //delay_ms(100);
   }   
   else if(Nev > 205)
   {
      output_high(PIN_A5); // ON red
      delay_ms(1000);
      delay_ms(1000);
      delay_ms(1000);
      delay_ms(1000);
      delay_ms(1000);
      break;
   }
}

void Reverse()
{
   restart_wdt();
   float MA;
   MA = 0;

   output_high(PIN_C4); // reverse ON
   delay_ms(1000);
   MA = LDR();
   
    if(MA <= 185)
    {
      output_low(PIN_C4);
      output_high(PIN_A1); // green
      delay_ms(1000);
      delay_ms(1000);
      delay_ms(1000);
      delay_ms(1000);
      delay_ms(1000);
      //output_low(PIN_A1);
      break;
    }
    else if(MA > 185)
    {
      output_high(PIN_A5); // ON red
      delay_ms(1000);
      delay_ms(1000);
      delay_ms(1000);
      delay_ms(1000);
      delay_ms(1000);
      break;
   }
}

void main()
{

   setup_adc_ports(SAN0);
   setup_adc(ADC_CLOCK_INTERNAL);
   //set_tris_c(0x00);
   //set_tris_b(0x00);
   
   output_low(PIN_C0);  // Position
   output_low(PIN_C1);  // Stop
   output_low(PIN_C2);  // DI
   output_low(PIN_C3);  // Fog
   output_low(PIN_C4);  // Reverse
   output_low(PIN_A1);  // Green sign
   output_low(PIN_A5);  // Red sign
         
   //while(true)
   //{
      //restart_wdt();
      //delay_ms(500);
      presenca();
      delay_ms(100);
      STOP();
      delay_ms(100);
      DI();
      delay_ms(100);
      Fog();
      delay_ms(100);
      Reverse();
      //delay_ms(500);   
      break;
   //}
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed May 03, 2023 7:42 pm     Reply with quote

The 16F18326 data sheet says in the WDT section:
Quote:
After a Reset, the default time-out period is two seconds.

You have several 5 second delays. The WDT is going to reset after 2 secs.

I'd advise you to disable WDT during development of your program.
Change the fuse to NOWDT.
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