Hi,
I am trying to get a series of LED's to light up when a qtc pressure pill is pressed, using a 16f684. I have adapted an existing ADC program to try and do this and have managed to get the LED's to light up. The problem however lies with the fact that I can not seem to get the LED's to stay at the light level wanted (using the pressure) once the QTC has been released.
I have made 4 PWM's using for loops to set the required LED brightness for each of the ADC values.
I have a basic knowledge in C programming and have tried several different ways of trying to get the light level to stay.
What do i need to do in order to keep the light level constant (indefinitely) until the QTC registers another pressure? Do I need to make a switch case/ an interupt? As previously stated my knowledge in C is limited. Any help would be greatly appreciated. The code is below;
Code:
#include <pic.h>
#include <stdio.h>
#include <string.h>
/* cFlash.c - Simple C Program to Flash an LED on a PIC16F684
RA4 - LED Positive Connection
RA5 - LED Negative Connection
*/
__CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT \
& UNPROTECT & BORDIS & IESODIS & FCMDIS);
int i, j;
//#define E RC4
//#define RS RC5
//#define QTC1 RA0 //Defines QTC1 as RA0 input
//#define QTC2 RA4 //Defines QTC2 as RA4 input
//#define QTC3 RA3 //Defines QTC3 as RA3 input
const char display_portc[]= {0b00000001, 0b00000010, 0b00000100, 0b00001000, 0b00010000, 0b00100000};
const char display_porta[]= {0b00000001, 0b00000010, 0b00000100, 0b00001000, 0b00010000, 0b00100000};
int ADCresult= 0;
void manualPWM()
{
//while (1)
//while (ADCresult<= 60)
for (i = 0; i < 2; i++) // Simple 500ms Delay
for (j = 0; j < 10; j++);
PORTC= display_portc[0]; // LED On
for (i = 0; i < 10; i++) // Simple 500ms Delay
for (j = 0; j < 100; j++);
PORTC= display_portc[1]; // LED Off
//GODONE = 0;
}
void manualPWM2()
{
// while (1)
//while (ADCresult<= 120)
for (i = 0; i < 15; i++) // Simple 500ms Delay
for (j = 0; j < 5; j++);
PORTC= display_portc[0]; // LED On
for (i = 0; i < 10; i++) // Simple 500ms Delay
for (j = 0; j < 55; j++);
PORTC= display_portc[1]; // LED Off
//GODONE = 0;
}
void manualPWM3()
{
//while (1)
//while (ADCresult<= 180)
for (i = 0; i < 25; i++) // Simple 500ms Delay
for (j = 0; j < 10; j++);
PORTC= display_portc[0]; // LED On
for (i = 0; i < 10; i++) // Simple 500ms Delay
for (j = 0; j < 55; j++);
PORTC= display_portc[1]; // LED Off
//GODONE = 0;
}
void manualPWM4()
{
// while (1)
//while (ADCresult<= 240)
for (i = 0; i < 255; i++) // Simple 500ms Delay
for (j = 0; j < 129; j++);
PORTC= display_portc[0]; // LED On
for (i = 0; i < 10; i++) // Simple 500ms Delay
for (j = 0; j < 1; j++);
PORTC= display_portc[1]; // LED Off
//GODONE = 0;
}
main()
{
int ADCresult= 0;
PORTA = 1;
PORTC = 0;
CMCON0 = 7; // Turn off Comparators
ANSEL = 0b00000100; //Pin 11- RA2 as analogue input
TRISA= 0b0000100; //PortA input
TRISC = 0; //PortC as output
ADCON0= 0b00001001; //Result left justified format, voltage reference= VDD,
//AN2= analogue input, initiates ADC and waiting to start conversion.
ADCON1= 0b00010000; // For 4 Mhz, 8Tosc= 2 uS, which > 1.6 uS, the minimum requirement
while(1) // Loop Forever
{
GODONE = 1; //Start conversion.
while (GODONE== 1); //Wait until conversion is done.
ADCresult= ADRESH; //Transfer from ADRESH to ADCresult.
if(ADCresult< 255) PORTA= 0;
if(ADCresult<= 30) PORTC= display_portc[0];
else if(ADCresult<= 60)
{
manualPWM();
}
else if(ADCresult<= 120)
{
manualPWM2();
}
else if(ADCresult<= 180)
{
manualPWM3();
}
else if(ADCresult<= 240)
{
manualPWM4();
}
} // End cFlash
}
A big thanks to anyone that can help point me in the right direction as have been working on this for a long time without any real success.
O.lewis