• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Scrolling menu trouble

Status
Not open for further replies.

Samaan

New Member
Hi. This is a system that reads 8 analogue inputs from the ADC converter and samples it at 3 different rates. It also allows you to download the data later. In the code below, the download part has been removed because the coding is very lengthy. So to simulate that, just the wordings with a delay is used. Now the problem is, that I cannot make the words stay without blinking. And another problem is that when I go into a task, then return back, it would just automatically keep d

Code:
#include <p18f4520.h>
#include <delays.h>
#include <stdio.h>
#include <system4520.h>
#include <adc.h>
#include <pwm.h>
#include <usart.h>

int adc (int);
void buzz ();
int Timer_Interrupt(int);
void Write_Voltage(int);
void readstored();
void fastfreq();
void mediumfreq();
void lowfreq();
void welcome();
void sysinit();
void download();

int sample_rate;
int nstate = st_welcome;

enum state_machine
{
	st_welcome = 0,
	st_lowfreq = 1,
	st_mediumfreq = 2,
	st_fastfreq = 3,
	st_readstored =4,
};

void main(void)
{
	TRISAbits.TRISA4 = 1;												// RA4 = input
	TRISBbits.TRISB0 = 1;
	PORTBbits.RB0 =1;
	PORTAbits.RA4 = 1;
	lcd_init();

	while(1)
	{
		switch(nstate)
		{
			case st_welcome:
				welcome();
				break;
			case st_lowfreq:
				lowfreq();
				break;
			case st_mediumfreq:
				mediumfreq();
				break;
			case st_fastfreq:
				fastfreq();
				break;
			case st_readstored:
				readstored();
				break;
		}
	}
}

void download(void)
{
	char download_text1[16] = "DOWNLOADING...  ";
	
	clear_screen();
	cursor_set(0,0);
	WriteS(download_text1);

	Delay10KTCYx (30);
}

void sysinit(void)
{
	TRISAbits.TRISA4=1;
	TRISBbits.TRISB0=1;
	TRISB=0;
	lcd_init();
}

void welcome(void)
{
	char msg1[16]="Weclome to Data ";
	char msg2[16]="  ACQ SYSTEM    ";
	cursor_set(0,0);
	WriteS(msg1);
	cursor_set(1,0);
	WriteS(msg2);
	Delay10KTCYx(240);
	clear_screen();
	nstate = st_lowfreq;
}

void lowfreq(void)
{
	char msg1[16]="SAMPLE FREQ: 0.2s";
	char msg2[16]="S2:NEXT S3:SLECT";
	cursor_set(0,0);	
	WriteS(msg1);
	cursor_set(1,0);
	WriteS(msg2);
	Delay10KTCYx(90);

	if(PORTBbits.RB0 == 0 || PORTAbits.RA4 == 0)
	{
		if(PORTBbits.RB0 == 0)
		{
			PORTBbits.RB0 == 1;
			while(1)
			{
				adc(10);
				nstate = st_readstored;
				clear_screen();
				break;
			}
		}
		else if(PORTAbits.RA4 == 0)
		{
			PORTAbits.RA4 == 1;
			nstate = st_mediumfreq;
			clear_screen();
		}
	}
}

void mediumfreq(void)
{
	char msg1[16]="SAMPLE FREQ:  1 s";
	char msg2[16]="S2:NEXT S3:SLECT";
	cursor_set(0,0);
	WriteS(msg1);
	cursor_set(1,0);
	WriteS(msg2);
	Delay10KTCYx(90);

	if(PORTBbits.RB0 == 0 || PORTAbits.RA4 == 0)
	{
		if(PORTBbits.RB0 == 0)
		{
		PORTBbits.RB0 == 1;
			while(1)
			{
					adc(20);
					nstate = st_readstored;
					clear_screen();
					break;
			}
		}
		else if(PORTAbits.RA4 == 0)
		{
			PORTAbits.RA4 == 1;
			nstate = st_fastfreq;
			clear_screen();
		}
	}
}

void fastfreq(void)
{
	char msg1[16]="SAMPLE FREQ:   2s";
	char msg2[16]="S2:NEXT S3:SLECT";
	cursor_set(0,0);
	WriteS(msg1);
	cursor_set(1,0);
	WriteS(msg2);
	Delay10KTCYx(90);

	if(PORTBbits.RB0 == 0 || PORTAbits.RA4 == 0)
	{
		if(PORTBbits.RB0 == 0)
		{
		PORTBbits.RB0 == 1;
			while(1)
			{
				adc(30);
				nstate = st_readstored;
				clear_screen();
				break;
			
			}
		}
		else if(PORTAbits.RA4 == 0)
		{
			PORTAbits.RA4 == 1;
			nstate = st_readstored;
			clear_screen();
		}
	}
}

void readstored(void)
{
	char msg1[16]="READ DATA EEPROM";
	char msg2[16]="S2:NEXT S3:SLECT";
	cursor_set(0,0);
	WriteS(msg1);
	cursor_set(1,0);
	WriteS(msg2);
	Delay10KTCYx(90);

	if(PORTBbits.RB0 == 1 || PORTAbits.RA4 == 1)
	{
		if(PORTBbits.RB0 == 0)
		{
		PORTBbits.RB0 == 1;
			while(1)
			{
				download();
				nstate = st_lowfreq;
				clear_screen();
				break;
			}
		}
		else if(PORTAbits.RA4 == 0)
		{
			PORTAbits.RA4 == 1;
			nstate = st_lowfreq;
			clear_screen();
		}
	}
}

void Write_Voltage(int hex_value)
{
	int t1;
	
	t1 = hex_value/100;
	if(!(t1 == '0'))
	{
		WriteC(getchar(t1));
	}
	else
	{
		WriteC(' ');
	}
	WriteC('.');
	t1 = ((hex_value%100)/10);
	WriteC(getchar(t1));
	t1 = ((hex_value%100)%10);
	WriteC(getchar(t1));
	WriteC(' ');
}

int Timer_Interrupt(int count)
{
		
	TMR0H = count/256;
	TMR0L = count%256;
	
	INTCONbits.TMR0IF = 0;	// reset TMR0 overflow Interrupt Flag
	T0CONbits.TMR0ON = 1; 	// start TMR0

	//wait for overflow i.e. TMR0 interrupt flag
	while(INTCONbits.TMR0IF == 0) // this is Polling
	{
		;
	}
}

void buzz (void)
{
	Delay100TCYx(50);
	OpenPWM1(158);
	SetDCPWM1(50);

	Delay100TCYx(50);
	OpenPWM1(158);
	SetDCPWM1(50);

	SetDCPWM1(0);
}

int adc (int sample_rate)
{
	char ADC_text[16] = "VOLTAGE   =    ";
	char ADC_text2[16] = "HLD RA4 TO EXIT";
	int adc0, adc1, adc2, adc3, adc4, adc5, adc6, adc7, count;
	

	// All input ports for the A/D converter
	TRISAbits.TRISA0 = 1;
	TRISAbits.TRISA1 = 1;
	TRISAbits.TRISA2 = 1;
	TRISAbits.TRISA3 = 1;
	TRISAbits.TRISA5 = 1;
	TRISEbits.TRISE0 = 1;
	TRISEbits.TRISE1 = 1;
	TRISEbits.TRISE2 = 1;
	TRISBbits.TRISB0 = 0;
	clear_screen();
	//ADCON1 to -15 VREF
	ADCON1 = 0b00000010;

	//Start ADC conversion
	WriteS(ADC_text);
	cursor_set(1,0);
	WriteS(ADC_text2);

//	sample_rate = 10;


	if(sample_rate == 10)
	{
		T0CON = 0x00000001;
		count = 24036;
	}
	else if(sample_rate == 20)
	{
		T0CON = 0x00000010;
		count = 24286;
	}
	else
	{
		T0CON = 0x00000010;
		count = 3036;
	}
	
	while (1)
	{
		// Give the ADC time to get ready.
      	Delay100TCYx (2);
		
		//On A/D converter
		ADCON0bits.GO = 1;

		while (ADCON0bits.GO);
		
		ADCON0 = 0b00000011;
		
		Delay100TCYx (2);
		adc0 = ADRESH;
		
		cursor_set(0,8);
		WriteC('0');
		cursor_set(0,12);
		Write_Voltage((adc0)/0.51);
		Delay100TCYx (100);
		
		
		Timer_Interrupt(count);


		ADCON0 = 0b00000111;	
		
		Delay100TCYx (2);
		adc1 = ADRESH;

		cursor_set(0,8);
		WriteC('1');
		cursor_set(0,12);
		Write_Voltage((adc1)/0.51);
		Delay100TCYx (100);
	
		
		Timer_Interrupt(count);
		

		ADCON0 = 0b00001011;

		
		Delay100TCYx (2);
		adc2 = ADRESH;
		
		cursor_set(0,8);
		WriteC('2');
		cursor_set(0,12);
		Write_Voltage((adc2)/0.51);
		Delay100TCYx (100);
		

		Timer_Interrupt(count);

		
		ADCON0 = 0b00001111;	
		
		Delay100TCYx (2);
		adc3 = ADRESH;

		cursor_set(0,8);
		WriteC('3');
		cursor_set(0,12);
		Write_Voltage((adc3)/0.51);
		Delay100TCYx (100);
		

		Timer_Interrupt(count);


		ADCON0 = 0b00010011;
		
		Delay100TCYx (2);
		adc4 = ADRESH;
		
		cursor_set(0,8);
		WriteC('4');
		cursor_set(0,12);
		Write_Voltage((adc4)/0.51);
		Delay100TCYx (100);
		
		
		Timer_Interrupt(count);
		

		ADCON0 = 0b00010111;	
		
		Delay100TCYx (2);
		adc5 = ADRESH;

		cursor_set(0,8);
		WriteC('5');
		cursor_set(0,12);
		Write_Voltage((adc5)/0.51);
		Delay100TCYx (100);
	
		
		Timer_Interrupt(count);
		

		ADCON0 = 0b00011011;

		
		Delay100TCYx (2);
		adc6 = ADRESH;
		
		cursor_set(0,8);
		WriteC('6');
		cursor_set(0,12);
		Write_Voltage((adc6)/0.51);
		Delay100TCYx (100);
		

		Timer_Interrupt(count);

		
		ADCON0 = 0b00011111;	
		
		Delay100TCYx (2);
		adc7 = ADRESH;

		cursor_set(0,8);
		WriteC('7');
		cursor_set(0,12);
		Write_Voltage((adc7)/0.51);
		Delay100TCYx (100);
		
		Timer_Interrupt(count);
		
		if(((adc0)/0.51) > 350 || ((adc1)/0.51) > 350 
		|| ((adc2)/0.51) > 350 || ((adc3)/0.51) > 350 
		|| ((adc4)/0.51) > 350 || ((adc5)/0.51) > 350 
		|| ((adc6)/0.51) > 350 || ((adc7)/0.51) > 350)
		{
			buzz();
		}
		
		if(PORTAbits.RA4 == 0)
		break;
	}
	
	clear_screen();

}
MPLAB IDE v8.76, C18

Hope to hear from you soon.
 
Last edited:
Status
Not open for further replies.

EE World Online Articles

Loading

 
Top