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 support@ccsinfo.com

PIC12F675 Battery LVD and HVD Using Only One Pin

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



Joined: 25 May 2018
Posts: 43
Location: Nigeria

View user's profile Send private message

PIC12F675 Battery LVD and HVD Using Only One Pin
PostPosted: Thu Sep 06, 2018 7:18 am     Reply with quote

This code is to activate and deactivate a Relay or turn on or turn off a LED during High voltage and Low voltage protection. I have done it making use of AN0(LVD) and AN1(HVD) of 16F675 as the ADC Inputs and GP2(LVD) and GP4(HVD) as the outputs.

GP2 will turn on a relay through a transistor Q1 driver while in normal rang
GP4 will also turn a transistor Q2 which in turn turn off Q1 during HVD state.

Below is the working code and the Proteus simulator file. I have tested it on hardware and it is working just as in Proteus SIM.

https://drive.google.com/file/d/1lY1ZpaZbxULY2BhwMZqBasmJp1IZIO0h/view?usp=sharing


Code:



#include <12F675.h>
#device adc=10
#use delay(clock=4000000)
#fuses NOWDT,INTRC_IO,PUT,NOPROTECT,BROWNOUT,NOMCLR,NOCPD

#define LED1 PIN_A2
#define LED2 PIN_A4

long ad;
float ANALOG;
 
void GetADC(int channel)
 {
   set_adc_channel(channel);             // Set A/D channel
   delay_us(20);                         // Delay acquisition time
   ad = read_adc();                      // Read A/D channel into ad variable
   ANALOG = (float)ad * (5.00/1024);      // Convert 10-bit reading
 }


void init()                               // Hardware initialization
 {
   setup_adc(ADC_CLOCK_DIV_8);            // A/D clock/8 @4MHz,Tad=2uS
   setup_adc_ports(sAN0 | sAN1);
 }



 void main()
 {
   init();                                 // Configure peripherals/hardware
   while(1)                                // Continuous loop
   {
     GetADC(0);
     if (ANALOG <= 0.90)
       {
         output_bit(LED1,0);
       }
     if (ANALOG >= 1.00)
             output_bit(LED1,1);

     GetADC(1);
     if (ANALOG <= 1.10)
       {
         output_bit(LED2,0);
       }
     if (ANALOG >= 1.20)
             output_bit(LED2,1);
     
       }
 }


My Problem now is, I want to use only AN0 for the two functions, that is one pin in, one pin out, and the the relay will activate and deactivate as in circuit one.

I am very new to the MCU world. I have less knowledge in them but I am trying now to know more and that's why I am playing around with this Chip. I have tried written anything the Compiler could not find error in and tested them and nothing seemed to work normal.

In this second code and circuit, LVD Part works well but the Hysterises for the HVD is missing, I have tried anyhow to insert it to the code and fail all the times. It goes off at 1.20V (HVD) and returns back just anything less than 1.20V. I want it to remain low until it comes down to 1.10V before the relay comes on again.

Below is the DRIVE link and the Proteus SIM File


https://drive.google.com/file/d/14ex2y8E2WNjKCjm9VEwRZSvDTUDQpO-Q/view?usp=sharing





Code:
#include <12F675.h>
#device adc=10
#use delay(clock=4000000)
#fuses NOWDT,INTRC_IO,PUT,NOPROTECT,BROWNOUT,NOMCLR,NOCPD

#define LED PIN_A2


long ad;
float ANALOG;
 
void GetADC(int channel)
 {
   set_adc_channel(channel);             // Set A/D channel
   delay_us(20);                         // Delay acquisition time
   ad = read_adc();                      // Read A/D channel into ad variable
   ANALOG = (float)ad * (5.00/1024);      // Convert 10-bit reading
 }


void init()                               // Hardware initialization
 {
   setup_adc(ADC_CLOCK_DIV_8);            // A/D clock/8 @4MHz,Tad=2uS
   setup_adc_ports(sAN0);
 }



 void main()
 {
   init();                                 // Configure peripherals/hardware
   while(1)                                // Continuous loop
   {
     GetADC(0);
     
     if (ANALOG <= 0.90)
       {
         output_bit(LED,0);
       }
     else if (ANALOG >= 1.20)
     {
        output_bit (LED,0);
     }
     else if (ANALOG >= 1.00)
 
     {
     output_bit(LED,1);
     }
   
       }
 }
       
[/code][/url]
_________________
All is well even in the well!
mcmsat



Joined: 25 May 2018
Posts: 43
Location: Nigeria

View user's profile Send private message

PostPosted: Thu Sep 06, 2018 7:21 am     Reply with quote

Please can anybody here help me to correct this code so that the HVD remains active until voltage reads 1.10V?

I appreciate your help in advance.

Thanks.
_________________
All is well even in the well!
temtronic



Joined: 01 Jul 2010
Posts: 9081
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Sep 06, 2018 8:23 am     Reply with quote

OK, I can't help with the Proteus part of your project ,as we all know Proteus is broken. Please read Sticky above PIC101...

As for your code, using floating point math takes a lot of time and code space and there are potential errors during the type conversion.
You're far better off using integer math. Faster and more accurate.
Also since you want to control a relay, be sure to use 'snubbing' on the relay coil. You'll need a very stable power supply as you're using VDD as the ADC reference ,so ANY variation in VDD ,WILL affect the ADC reading and your control program.
As to 'hysterisis', it's been my preference to add/subtract a separate value for that . say 'setpoint - hysterisis'. That way you can easily change the amount of hysterisis.
mcmsat



Joined: 25 May 2018
Posts: 43
Location: Nigeria

View user's profile Send private message

PostPosted: Thu Sep 06, 2018 9:09 am     Reply with quote

@ temtronic, thanks for your reply.

The proteus thing is just for a schematic reference. I can equally represent that with a jpeg picture which I will do now.

Please, can you give me a modification of my code to do this? I will learn to do this gradually. I just started it.

The relay is also as a reference. This code is not for any application, I just want to understand PIC Microcontrollers coding through practice.

I want code to make the first circuit control the LED "state" just like the second circuit

[img]https://drive.google.com/open?id=11ZL7pxLKXnqSF4AfVNWYd9zn0xK5DOhP[/img]


[img]https://drive.google.com/open?id=1jCodO6TCJBXmO_tkSBaNFC1xjZNTusdO[/img]
_________________
All is well even in the well!
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