#include <inttypes.h>
#include <avr/io.h>
#include <stdlib.h>
#include <avr/interrupt.h>
#include <math.h>
#include <avr/pgmspace.h>
#include <stdio.h>
#include <util/delay.h>
#include <string.h>
#include <avr/common.h>
#include "Mrlcd.h"
void Stepper_Motor_CW(void);
void Stepper_Motor_CCW(void);
void Initialize_ADC(void);
void pwm_set(void);
void Initialize_Interrupt(void);
void Stepper_Motor_STOP(void);
#define YELLOW _BV(PB4)
#define BLACK _BV(PB5)
#define ORANGE _BV(PB6)
#define BROWN _BV(PB7)
#define RED _BV(PB0)
#define GREEN _BV(PB1)
#define DELAY delay(80) // _delay_loop_2(Reading1)
float adc_Value;
float adcResult[10];
volatile float Reading = 0.00;
int Reading1 ;
int Pressed = 0;
int Pressed_Confidence_Level = 0;
int Released_Confidence_Level = 0;
int main(void)
{
//Init_Ports();
//Init_Lcd();
Initialize_Interrupt();
Initialize_ADC();
pwm_set();
//sei();
DDRB |= (1<<PB0 | 1<<PB1 | 1<<PB4 | 1<<PB5 | 1<<PB6 | 1<<PB7); /* Enable output on all of the B pins */
PORTB &= ~(1<<PB0 | 1<<PB1 | 1<<PB4 | 1<<PB5 | 1<<PB6 | 1<<PB7);
DDRD |= (1<<PD7);
DDRD &= ~(1<<PD3);
PORTD |= 1<<PIND3;
while(1)
{
//delay(50);
for(uint16_t i=0;i<25;i++)
{
Stepper_Motor_CW(); //Step Clock wise
delay(20);
}
for(uint16_t i=0;i<25;i++)
{
Stepper_Motor_CCW() ; //Step Counter Clock wise
delay(20);
}
}
}
ISR(ADC_vect)
{
uint8_t theLow =ADCL;
uint16_t tenBitValue = ADCH<<2 | theLow>>6;
Reading = tenBitValue;
Reading1 = (Reading/4);
//OCR0 = Reading1;
OCR2 = Reading1;
delay(40);
Send_A_String(" Speed Value : ");
newline();
Send_A_String(" ");
float adc_read = Reading1;
int d1 = adc_read; // Get the integer part (25).
float f2 = adc_read - d1; // Get fractional part (0.0123).
int d2 = trunc(f2 * 10000); // Turn into integer (123).
sprintf (adcResult,"%d.%04d%", d1, d2);
//itoa(temp,adcResult,10);
Send_A_String(adcResult);
//Send_A_String(" oC");
Send_A_String(" ");
delay(20);
ADCSRA |= 1<<ADSC; // Start Conversion
}
void Initialize_ADC()
{
ADCSRA |= 1<<ADPS2 | 1<<ADPS1| 1<<ADPS0; // 16 Mhz EXT_OCC, so enable pre-scaler 128 Division Factor
ADMUX |= 1<<ADLAR; // 8 bit operation "left Shift"
//ADMUX |= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 ;
ADMUX |= 3;
//ADMUX |= 1<<REFS0;//AVCC with external capacitor at AREF pin
//ADMUX &= ~(1<<REFS1);//AVCC with external capacitor at AREF pin
SFIOR &= ~((1<<ADTS2) |(1<<ADTS1) |(1<<ADTS0));// FREE wheeling Mode
ADCSRA |= 1<<ADATE;
ADCSRA |= 1<<ADIE; //Enable Interrupt in ADC
ADCSRA |= 1<<ADEN; //Turn on ADC Feature
sei();
ADCSRA |= 1<<ADSC; // Start Conversion
}
//void pwm_set(unsigned char pulse_width)
void pwm_set()
{
//TCCR0 = (1<<COM01)|(1<<WGM00)|(1<<CS00)|(1<<CS01)|(1<<WGM01);
TCCR2 = (1<<COM21)|(1<<WGM20)|(1<<CS22)|(1<<WGM21);
}
void Initialize_Interrupt()
{
MCUCR |= (1<<ISC11);
MCUCR |= (1<<ISC10);
//MCUCR |= (1<<ISC01);
//MCUCR |= (1<<ISC00);
GICR |= (1<<INT1);
//GICR |= (1<<INT0);
sei();
}
void Stepper_Motor_CCW()
{
PORTB = YELLOW;
DELAY;
PORTB = BLACK;
DELAY;
PORTB = ORANGE;
DELAY;
PORTB = BROWN;
DELAY;
PORTB = GREEN;
DELAY;
}
void Stepper_Motor_CW()
{
PORTB = BROWN;
DELAY;
PORTB = ORANGE;
DELAY;
PORTB = BLACK;
DELAY;
PORTB = YELLOW;
DELAY;
PORTB = RED;
DELAY;
}
void Stepper_Motor_STOP()
{
PORTB &= ~ BROWN;
PORTB &= ~ ORANGE;
PORTB &= ~ BLACK;
PORTB &= ~ YELLOW;
PORTB &= ~ RED;
PORTB &= ~ GREEN;
}