0% found this document useful (0 votes)
5 views7 pages

String Old

The document is a C program for an embedded system that utilizes ADC, timers, and interrupts to read a potentiometer value and control an LED based on that value. It initializes system I/O, sets up ADC and USART communication, and implements a main loop that triggers actions based on external interrupts. The program calculates a moving average of the potentiometer readings to determine the LED state and manages timing with a timer interrupt service routine.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views7 pages

String Old

The document is a C program for an embedded system that utilizes ADC, timers, and interrupts to read a potentiometer value and control an LED based on that value. It initializes system I/O, sets up ADC and USART communication, and implements a main loop that triggers actions based on external interrupts. The program calculates a moving average of the potentiometer readings to determine the LED state and manages timing with a timer interrupt service routine.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

#include <io.

h>
#include <delay.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>

///////////////////////////////////////////////////////////////////// IO and VARS,


Consts ////////////////////////////////////////////////////////////////////////////
//////
#define OUTPUT_MODE_PORT 1
#define INPUT_MODE_PORT 0

#define ON 1
#define OFF 0

//////////////////////////
#define LED_OUT PORTD.3
#define POT_INPUT 2
#define TRIG_IN PIND.2
#define SIGNAL_OUT PORTB.2
////////////////////////

char tmr_fn=0;
char TRIG_SENSOR=OFF;
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
//////
void init_system_IO()
{
DDRD.3=OUTPUT_MODE_PORT;
DDRD.2=INPUT_MODE_PORT;
DDRB.2=OUTPUT_MODE_PORT;
}

// Voltage Reference: AVCC pin


#define ADC_VREF_TYPE ((0<<REFS1) | (1<<REFS0) | (0<<ADLAR))

// Read the AD conversion result


unsigned int read_adc(unsigned char adc_input)
{
ADMUX=adc_input | ADC_VREF_TYPE;
// Delay needed for the stabilization of the ADC input voltage
delay_us(10);
// Start the AD conversion
ADCSRA|=(1<<ADSC);
// Wait for the AD conversion to complete
while ((ADCSRA & (1<<ADIF))==0);
ADCSRA|=(1<<ADIF);
return ADCW;
}
unsigned int GET_AVG_ANALOG(int avg_, char ch_)
{
unsigned char i=0;
unsigned long int AVG_BUFFER=0;

for(i=0;i!=avg_;i++)
{
AVG_BUFFER=(unsigned long int)read_adc(ch_)+AVG_BUFFER;
delay_ms(1);
}

return (unsigned int) AVG_BUFFER/avg_;


}

void START_TIMER(unsigned int tm)


{

tmr_fn=0;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 1000.000 kHz
// Mode: Normal top=0xFFFF
// OC1A output: Disconnected
// OC1B output: Disconnected
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer Period: 65.536 ms
// Timer1 Overflow Interrupt: On
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=(0<<COM1A1) | (0<<COM1A0) | (0<<COM1B1) | (0<<COM1B0) | (0<<WGM11) |
(0<<WGM10);
TCCR1B=(0<<ICNC1) | (0<<ICES1) | (0<<WGM13) | (0<<WGM12) | (0<<CS12) | (1<<CS11) |
(0<<CS10);

TCNT1=tm;

ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
}

void STOP_TIMER()
{

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer1 Stopped
// Mode: Normal top=0xFFFF
// OC1A output: Disconnected
// OC1B output: Disconnected
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=(0<<COM1A1) | (0<<COM1A0) | (0<<COM1B1) | (0<<COM1B0) | (0<<WGM11) |
(0<<WGM10);
TCCR1B=(0<<ICNC1) | (0<<ICES1) | (0<<WGM13) | (0<<WGM12) | (0<<CS12) | (0<<CS11) |
(0<<CS10);
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

long map(long x, long in_min, long in_max, long out_min, long out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

char delay_us_timer(unsigned int d)


{

START_TIMER(0xffff-d);
while(tmr_fn==0){}
return tmr_fn;

}
// Timer1 overflow interrupt service routine
interrupt [TIM1_OVF] void timer1_ovf_isr(void)
{

//LED_OUT=!LED_OUT;
tmr_fn=1;
STOP_TIMER();
}

// External Interrupt 0 service routine


interrupt [EXT_INT0] void ext_int0_isr(void)
{

//LED_OUT=!LED_OUT;
TRIG_SENSOR=ON;

/////////////////////////////////////////////////////////// MAIN
PROGRAM ///////////////////////////////////////////////////////////////////////////
/////////////////////////////
void main(void)
{
#define MIN_PULSE 600
#define MAX_PULSE 16300

unsigned char STR[10];


unsigned int DELAY_VALUE=0;
unsigned int i=0;
unsigned int POT_VALUE=0;
unsigned int POT_PERCENT=0;
unsigned int fnl_tcnt=0;

unsigned int prv_currentValue=0;

/////////////// MMA ///////////////////////


const unsigned char averageCount = 10;
float movingAverage;
float movingAverageSum;

// Timer variables
unsigned long previousMillis = 0;
unsigned int printInterval = 500;
unsigned long startTime = 0;
unsigned long stopTime = 0;
unsigned int currentValue=0;
unsigned int x=0;
//////////////////////////////////////////
delay_ms(100);
init_system_IO();

// ADC initialization
// ADC Clock frequency: 1000.000 kHz
// ADC Voltage Reference: AVCC pin
ADMUX=ADC_VREF_TYPE;
ADCSRA=(1<<ADEN) | (0<<ADSC) | (0<<ADFR) | (0<<ADIF) | (0<<ADIE) | (0<<ADPS2) |
(1<<ADPS1) | (1<<ADPS0);
SFIOR=(0<<ACME);

// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: Off
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 9600
UCSRA=(0<<RXC) | (0<<TXC) | (0<<UDRE) | (0<<FE) | (0<<DOR) | (0<<UPE) | (0<<U2X) |
(0<<MPCM);
UCSRB=(0<<RXCIE) | (0<<TXCIE) | (0<<UDRIE) | (0<<RXEN) | (1<<TXEN) | (0<<UCSZ2) |
(0<<RXB8) | (0<<TXB8);
UCSRC=(1<<URSEL) | (0<<UMSEL) | (0<<UPM1) | (0<<UPM0) | (0<<USBS) | (1<<UCSZ1) |
(1<<UCSZ0) | (0<<UCPOL);
UBRRH=0x00;
UBRRL=0x33;

STOP_TIMER();

// Timer(s)/Counter(s) Interrupt(s) initialization


TIMSK=(0<<OCIE2) | (0<<TOIE2) | (0<<TICIE1) | (0<<OCIE1A) | (0<<OCIE1B) |
(1<<TOIE1) | (0<<TOIE0);

// External Interrupt(s) initialization


// INT0: On
// INT0 Mode: Falling Edge
// INT1: Off
GICR|=(0<<INT1) | (1<<INT0);
MCUCR=(0<<ISC11) | (0<<ISC10) | (1<<ISC01) | (0<<ISC00);
GIFR=(0<<INTF1) | (1<<INTF0);

#asm("sei")

for(i=0;i!=8;i++){LED_OUT=!LED_OUT;delay_ms(50);}
LED_OUT=OFF;

for ( x=0; x < averageCount; x++){ movingAverageSum = movingAverageSum +


read_adc(POT_INPUT);}
WDTCR=(1<<WDCE) | (1<<WDE) | (1<<WDP2) | (1<<WDP1) | (1<<WDP0);
WDTCR=(0<<WDCE) | (1<<WDE) | (1<<WDP2) | (1<<WDP1) | (1<<WDP0);
#asm("wdr")

while (1)
{
#asm("wdr")

///////////////////////////////////////////////////////////////////////////////////
//////
if(TRIG_SENSOR==ON)
{
TRIG_SENSOR=OFF;
#asm("wdr")

SIGNAL_OUT=1;
delay_us(600);
SIGNAL_OUT=0;

////////// GET_POT //////////


//POT_VALUE=GET_AVG_ANALOG(5,POT_INPUT);
POT_VALUE= read_adc(POT_INPUT);
movingAverageSum = movingAverageSum - movingAverage;
// Replace it with the current sample
movingAverageSum = movingAverageSum + POT_VALUE;
// Recalculate movingAverage
movingAverage = movingAverageSum / averageCount;
POT_VALUE=movingAverage;

if(POT_VALUE>1000){LED_OUT=ON;}else{LED_OUT=OFF;}
/////////////////////////////

delay_us(7600);

SIGNAL_OUT=1;
delay_us(240);
SIGNAL_OUT=0;

///////////////////////////////////////////////////
POT_VALUE=(unsigned int)POT_VALUE*13;
POT_VALUE=(unsigned int)13600-POT_VALUE;
delay_us_timer(POT_VALUE);
///////////////////////////////////////////////////

SIGNAL_OUT=1;
delay_us(240);
SIGNAL_OUT=0;
}
///////////////////////////////////////////////////////////////////////////////////
//////

You might also like