Can anybody convert this code into hex file in Code Vision AVR

Thread Starter

mahmud1994

Joined Jul 16, 2017
23
I am doing a project using atmega32 given by my course instructor. However i am not familiar with Code Vision AVR which i needed for coding purpose. i found out a code which is suitable for my project. Can anybody convert this source code into hex file in CV AVR. So that i can run it with circuit. I have tried to solve it by myself and fail to do so as this is not similar with the code writing process taught in my class, also it is a complex project to rotate stepper motor to the smallest particular angle possible. However it is showing error on all three #include file. Note. Here in atmega32 i used Port A as input. port b c d as output. Circuit is added.

#include <avr/io.h>
#include <util/delay.h>
#include <string.h>
//////////////// variable
declarations////////////////////////////

unsigned int angle=0,new_angle,set_angle=0,num_of_pulses,
rotate_reverse_flag=0;
///////////// function to send data to
LCD//////////////////////////
void senddata(unsigned char data)
{
_delay_ms(2); // wait for LCD to be ready
PORTD=(1<<PD0); // for data rs=1
PORTC=data; // put data byte on data-bus
PORTD=(1<<PD0)|(1<<PD1); // give strobe to En pin
PORTD=(1<<PD0)|(0<<PD1);
}
//////////////////// function to send command to
LCD////////////////
void sendcmd(unsigned char cmd)
{
_delay_ms(2); // wait for LCD to be ready
PORTD=(0<<PD0); // for data rs=0
PORTC=cmd; // put command byte on data-bus
PORTD=(1<<PD1); // give strobe to En pin
PORTD=(0<<PD1);
}
///////////// function to send string to LCD
//////////////////////
void printstr(unsigned char *s)
{
uint8_t l,i;
l = strlen(s); // get the length of string
for(i=0;i<l;i++)
{
senddata(*s); // write every char one by one
s++;
}
}
///////////////// function to display angle value on
LCD///////////////
void display_value(unsigned int t)
{
unsigned int t1,a;

unsigned char asci[3];
if(t>=100) // for angle in 3 digit
{
a=2;
while(t>=10) // separate all 3 digits
{
t1=t%10; // one by one
asci[a]=t1+0x30; // convert them into ASCII
t=t/10; // store them in array
a-- ;
}
asci[0]=t+0x30; // first digit of angle
}
else // for angle in2 digits
{
t1=t%10; // separate both digits
asci[2]=t1+0x30; // convert them in to ASCII
t=t/10;
asci[1]=t+0x30;
asci[0]=0x20; // take first digit as space
}
sendcmd(0xC0);
senddata(asci[0]); // send all digits one by one to
senddata(asci[1]); // LCD
senddata(asci[2]);
printstr(" deg ");
}
//////////////// function to initialize LCD
/////////////////////////
void lcd_init()
{
sendcmd(0x3E); // configure LCD dot pattern
sendcmd(0x0E); // LCD screen on cursor on
sendcmd(0x01); // clear LCD memory and home cursor
sendcmd(0x80); // set cursor to 1st line 1st column
printstr("Set Motor Angle:"); // display message
}
//////////////// function to increase angle by 15
//////////////////
void inc_angle()
{
if(angle<345) angle+=15; // increase angle till 345 degree
display_value(angle); // display new angle value
new_angle = angle; // update new angle value
}

////////////// function to decrease angle by 15 ////////////////
void dec_angle()
{
if(angle>0) angle-=15; // decrease angle till 0 degree
display_value(angle); // display new angle value
new_angle = angle; // update new angle value
}
///////////////// function to rotate motor till desire angle
//////////
void rotate_motor()
{
int i;
if(new_angle>set_angle) // if new entered angle is larger
{
num_of_pulses = (new_angle-set_angle) / 15; // set number of
rotate_reverse_flag=0; // pulses and clear flag to rotate
// motor CCW
}
else // if new entered angle is smaller
{
num_of_pulses = (set_angle-new_angle) / 30; // set number
rotate_reverse_flag=1; // of pulses and set flag to rotate
// motor CW
}
if(rotate_reverse_flag==0) // check if flag is clear
{
for(i=0;i<num_of_pulses;i++) // generate pulse sequence to
{ // rotate motor CCW
PORTB = 0x01;
_delay_ms(100);
PORTB = 0x02;
_delay_ms(100);
PORTB = 0x04;
_delay_ms(100);
PORTB = 0x08;
_delay_ms(100);
}
}
else // else if flag is set
{
for(i=0;i<num_of_pulses;i++) // generate pulse sequence to
{
PORTB = 0x08; // rotate motor CW
_delay_ms(100);
PORTB = 0x04;

_delay_ms(100);
PORTB = 0x02;
_delay_ms(100);
PORTB = 0x01;
_delay_ms(100);
}
}
set_angle = new_angle; // save angle value
}
/////////////////// main function starts here
/////////////////////////
int main(void)
{
DDRC=0xFF; // PORTB, PORTC and PORTD as
DDRD=0xFF; // an output ports
DDRB=0x0F;
DDRA=0x00; // PORT A as an input port
PORTC=0x00;
PORTD=0x00;
PORTB=0x0F;
lcd_init(); // initialize LCD
display_value(angle); // display angle as 0 degree
loop:while(PINA==0xF0); // loop until no button is pressed
switch(PINA) // when button is pressed
{
case 0xF1: // if 1st button is pressed
inc_angle(); // increment angle by 15 degree
_delay_ms(200); // key debounce delay
break; // get out of loop
case 0xF2: // same as above
dec_angle();
_delay_ms(200);
break;
case 0xF4: // for 3rd key
rotate_motor(); // rotate motor
break;
}
goto loop; // continuous loop
}
 

Attachments

Jony130

Joined Feb 17, 2009
5,213
Where did you found this code?
And traditional include statement looks like this:

C:
#include < avr/io.h >
#include < util/delay.h >
#include < string.h >
 
Top