subrata_009
Newbie level 2
Code:
#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;
}
Dear all,
I am facing some problem with my code
I am driving a stepper motor and 2 LED in port B, each LED is indicating direction of motor.
In port A ADC3 PIN , I am using this pin as an input for my potentiometer signal.
In port D PIN D7, I am using to output data of potentiometer through a led.
Now the problem is
a) when the below condition in function : void Initialize_ADC()
ADCSRA |= 1<<ADSC; // Start Conversion is enabled
then stepper motor is not running. Potentiometer Input and LED in Pin D7 is working fine.
b) when the below condition in function : void Initialize_ADC()
//ADCSRA |= 1<<ADSC; // Start Conversion is Disabled
then stepper motor is running perfectly and Potentiometer Input and LED in Pin D7 is not working as ADSC is disabled.
I want both to RUN perfectly, please help to solve the issue.....
Last edited by a moderator: