ADC formula question

Status
Not open for further replies.

skeeterb

Member
I need help with a formula for the ADC of a PIC 16F877A. I want to use it to adjust the timing in milliseconds instead of seconds. I tried a formula which I have gotten before and used it in a few of my programs I have designed that use the ADC to control timing. The formula I have currently goes from 0 - 60 seconds, but I need to go from 0 - 1000 ms. The formula I have is
(Result + 1) / 17. I have tried using a /1000 but it only works for the lower settings. Can anyone suggest a formula that I can use? Thanks for your help. I am posting the code for the program below.

Code:
#define MX_PIC

//Defines for microcontroller
#define P16F877A
#define MX_EE
#define MX_EE_TYPE2
#define MX_EE_SIZE 256
#define MX_SPI
#define MX_SPI_C
#define MX_SPI_SDI 4
#define MX_SPI_SDO 5
#define MX_SPI_SCK 3
#define MX_UART
#define MX_UART_C
#define MX_UART_TX 6
#define MX_UART_RX 7
#define MX_I2C
#define MX_MI2C
#define MX_I2C_C
#define MX_I2C_SDA 4
#define MX_I2C_SCL 3
#define MX_PWM
#define MX_PWM_CNT 2
#define MX_PWM_TRIS1 trisc
#define MX_PWM_1 2
#define MX_PWM_TRIS2 trisc
#define MX_PWM_2 1

//Functions
#define MX_CLK_SPEED 5000000
#ifdef _BOOSTC
#include <system.h>
#endif
#ifdef HI_TECH_C
#include <pic.h>
#endif

//Configuration data
#ifdef _BOOSTC
#pragma DATA 0x2007, 0x3ffa
#endif
#ifdef HI_TECH_C
__CONFIG(0x3ffa);
#endif

//Internal functions
#include "C:\Flowcode V4\FCD\internals.h"

//Macro function declarations


//Variable declarations
char FCV_SWITCH1;
char FCV_MODESET;
char FCV_SWITCH2;
short FCV_RETURNDELAY;
char FCV_COUNT;
char FCV_GOINGUP;



//Macro implementations

void main()
{
	
	//Initialisation
	adcon1 = 0x07;


	//Interrupt initialisation code
	option_reg = 0xC0;


	//Comment:
	//This program is supposed to as closely as possible the color changes
	//in the plumbob that hangs above your sims in the Electronic Arts "The
	//Sims" games. It's not exactly like you would see in the game, but I
	//worked it to get as close as I could using a fairly simple program.


	//Comment:
	//Your mood starts at the highest level, which is full green. The mood
	//tends to go down as needs are not met or as in "The Sims 3" game, you
	//get bad moodlets, which decrease your mood.


	//Calculation
	//Calculation:
	//  Count = 0
	//  ReturnDelay = 1000
	FCV_COUNT = 0;
	FCV_RETURNDELAY = 1000;
	

	//Comment:
	//The above command sets the mood to full green and sets the delay at
	//which the program waits until it can move on to its next command. I 
	//thought about using an ADC and trying to figure out a formula to allow
	//to adjust the timing, but still haven't worked it out yet.


	//Loop
	//Loop: While 1
	while (1)
	{
		//Decision
		//Decision: Modeset > 0?
		if (FCV_MODESET > 0)
		{
			//Connection Point
			//Connection Point: [A]: A
FCC_Main_A:
;


			//Input
			//Input: A1 -> Switch2
			trisa = trisa | 0x02;
			FCV_SWITCH2 = ((porta & 0x02) == 0x02);


			//Decision
			//Decision: Switch2 > 0?
			if (FCV_SWITCH2 > 0)
			{
				//Decision
				//Decision: Count = 0?
				if (FCV_COUNT == 0)
				{
					//Goto Connection Point
					//Goto Connection Point: [A]: A
					goto FCC_Main_A;


				} else {
					//Calculation
					//Calculation:
					//  Count = Count - 1
					FCV_COUNT = FCV_COUNT - 1;
					

					//Decision
					//Decision: GoingUp > 0?
					if (FCV_GOINGUP > 0)
					{
					} else {
						//Calculation
						//Calculation:
						//  GoingUp = 1
						FCV_GOINGUP = 1;
						

					}


				}


			} else {
				//Input
				//Input: A0 -> Switch1
				trisa = trisa | 0x01;
				FCV_SWITCH1 = ((porta & 0x01) == 0x01);


				//Decision
				//Decision: Switch1 > 0?
				if (FCV_SWITCH1 > 0)
				{
					//Decision
					//Decision: Count = 8?
					if (FCV_COUNT == 8)
					{
						//Goto Connection Point
						//Goto Connection Point: [A]: A
						goto FCC_Main_A;


					} else {
						//Calculation
						//Calculation:
						//  Count = Count + 1
						FCV_COUNT = FCV_COUNT + 1;
						

						//Decision
						//Decision: GoingUp > 0?
						if (FCV_GOINGUP > 0)
						{
							//Calculation
							//Calculation:
							//  GoingUp = 0
							FCV_GOINGUP = 0;
							

						}


					}


				} else {
					//Goto Connection Point
					//Goto Connection Point: [A]: A
					goto FCC_Main_A;


				}


			}


		}


		//Switch
		//Switch: Count?
		switch (FCV_COUNT)
		{
			case 1:
			{
				//Output
				//Output: 0x77 -> PORT B
				trisb = 0x00;
				portb = 0x77;


				//Output
				//Output: 0x11 -> PORT C
				trisc = 0x00;
				portc = 0x11;


				//Delay
				//Delay: ReturnDelay ms
				delay_ms(FCV_RETURNDELAY);


				break;
			}

			case 2:
			{
				//Output
				//Output: 0x33 -> PORT B
				trisb = 0x00;
				portb = 0x33;


				//Output
				//Output: 0x33 -> PORT C
				trisc = 0x00;
				portc = 0x33;


				//Delay
				//Delay: ReturnDelay ms
				delay_ms(FCV_RETURNDELAY);


				break;
			}

			case 3:
			{
				//Output
				//Output: 0x11 -> PORT B
				trisb = 0x00;
				portb = 0x11;


				//Output
				//Output: 0x77 -> PORT C
				trisc = 0x00;
				portc = 0x77;


				//Delay
				//Delay: ReturnDelay ms
				delay_ms(FCV_RETURNDELAY);


				break;
			}

			case 4:
			{
				//Decision
				//Decision: GoingUp > 0?
				if (FCV_GOINGUP > 0)
				{
					//Output
					//Output: 0 -> PORT D
					trisd = 0x00;
					portd = 0;


					//Output
					//Output: 0xFF -> PORT C
					trisc = 0x00;
					portc = 0xFF;


				} else {
					//Output
					//Output: 0 -> PORT B
					trisb = 0x00;
					portb = 0;


					//Output
					//Output: 0xFF -> PORT C
					trisc = 0x00;
					portc = 0xFF;


				}


				//Delay
				//Delay: ReturnDelay ms
				delay_ms(FCV_RETURNDELAY);


				break;
			}

			case 5:
			{
				//Output
				//Output: 0x11 -> PORT D
				trisd = 0x00;
				portd = 0x11;


				//Output
				//Output: 0x77 -> PORT C
				trisc = 0x00;
				portc = 0x77;


				//Delay
				//Delay: ReturnDelay ms
				delay_ms(FCV_RETURNDELAY);


				break;
			}

			case 6:
			{
				//Output
				//Output: 0x33 -> PORT D
				trisd = 0x00;
				portd = 0x33;


				//Output
				//Output: 0x33 -> PORT C
				trisc = 0x00;
				portc = 0x33;


				//Delay
				//Delay: ReturnDelay ms
				delay_ms(FCV_RETURNDELAY);


				break;
			}

			case 7:
			{
				//Output
				//Output: 0x77 -> PORT D
				trisd = 0x00;
				portd = 0x77;


				//Output
				//Output: 0x11 -> PORT C
				trisc = 0x00;
				portc = 0x11;


				//Delay
				//Delay: ReturnDelay ms
				delay_ms(FCV_RETURNDELAY);


				break;
			}

			case 8:
			{
				//Output
				//Output: 0xFF -> PORT D
				trisd = 0x00;
				portd = 0xFF;


				//Output
				//Output: 0 -> PORT C
				trisc = 0x00;
				portc = 0;


				//Delay
				//Delay: ReturnDelay ms
				delay_ms(FCV_RETURNDELAY);


				break;
			}

			default:
			{
				//Decision
				//Decision: GoingUp > 0?
				if (FCV_GOINGUP > 0)
				{
					//Output
					//Output: 0xFF -> PORT B
					trisb = 0x00;
					portb = 0xFF;


					//Output
					//Output: 0 -> PORT C
					trisc = 0x00;
					portc = 0;


					//Calculation
					//Calculation:
					//  GoingUp = 0
					FCV_GOINGUP = 0;
					

				} else {
					//Output
					//Output: 0xFF -> PORT B
					trisb = 0x00;
					portb = 0xFF;


				}


				//Decision
				//Decision: Modeset = 1?
				if (FCV_MODESET == 1)
				{
				} else {
					//Calculation
					//Calculation:
					//  Modeset = 1
					FCV_MODESET = 1;
					

				}


				//Delay
				//Delay: ReturnDelay ms
				delay_ms(FCV_RETURNDELAY);


			}
		}


	}


	mainendloop: goto mainendloop;
}

void MX_INTERRUPT_MACRO(void)
{
}
 
Well the ADC reading goes from 0-1023.

Why not simply use the ADC value directly in your delay_ms() function?

That would give a variable delay from 0mS to 1023mS.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…