|
source code
/**********************************************************/
/* PIC18F4620 ????: 1?42 6???????, 1?L298N????,2??????AD?? */
/* 1??????????? */
#include <pic18.h>
__PROG_CONFIG(1,0xc200);
__PROG_CONFIG(2,0x001e);
__PROG_CONFIG(3,0x0100);
__PROG_CONFIG(4,0x0081);
__PROG_CONFIG(5,0xc00f);
__PROG_CONFIG(6,0xe00f);
__PROG_CONFIG(7,0x400f);
#define uchar unsigned char
#define uint unsigned int
#define seg1 PORTAbits.RA2
#define seg2 PORTAbits.RA3
#define seg3 PORTAbits.RA4
#define seg4 PORTAbits.RA5
#define reset_check PORTEbits.RE0//pull-up with a 4.7K res to Vcc
#define count_clr PORTEbits.RE1//pull-up with a 4.7K res to Vcc
const uchar table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
const uchar coil_active[]={0x01,0x05,0x04,0x06,0x02,0x0a,0x08,0x09};
static uchar wait_t=0,speed=100;
uint count;
uchar num0,num1,num2,num3;
void delay(uint z);
void Port_init(void);
void display(void);
void EEPROM_write(uchar wr_data,uchar wr_addr);
void timer0_init(void);
uchar EEPROM_read(uchar addr);
void forward(void);
void hyst_forward(void);
void main()
{
uint temp1,temp2;
Port_init();
timer0_init();
delay(10);
temp1=EEPROM_read(0x01);//Æô¶ˉ¶áè¡éÏò»′Îê1óñêÇ©μÄêy¾Y£»
temp2=EEPROM_read(0x02);//Æô¶ˉ¶áè¡éÏò»′Îê1óñêÇ©μÄêy¾Y£»
count=temp1*256+temp2;
while(1)
{
display();
if (reset_check==0)
{
delay(10);
if(reset_check==0)
{
count++;
hyst_forward();
EEPROM_write(count/256,0x01);
EEPROM_write(count%256,0x02);
if(count==10000)
count=0;
while(!reset_check);
}
}
else
forward();
if (count_clr==0)
{
delay(10);
if (count_clr==0)
{
count=0;
EEPROM_write(0,0x01);//clear the data from E2PROM
EEPROM_write(0,0x02);//clear the data from E2PROM
}
}
}
}
void delay(uint z)
{
uint x,y;
for(x=0;x<z;x++)
for(y=0;y<10;y++);
}
void Port_init(void)
{
TRISA=0x03;
ADCON1=0x0D;//select AN0&AN1 as analog input channel;
ADCON2=0x3A;//left justified, 20Tad, Tosc/32;
TRISD=0x00;
PORTD=0xff;//Numerical LED scanning;
TRISC=0x00;//Step-Motor winding power-up;
PORTC=0x00;
TRISE=0x07;//set the PORTE as input I/O;
}
void timer0_init(void)
{
INTCON=0xE0;//GIE, PEIE and timer0 overflow interrupt;
T0CON=0x08;
TMR0H=(65535-1000)/256; //set the time to be 1ms;
TMR0L=(65535-1000)%256; //set the time to be 1ms;
TMR0ON=1;
}
void display(void)
{
num0=count/1000;
num1=count%1000/100;
num2=count%100/10;
num3=count%10;
PORTD=table[num0];
seg1=0;
delay(10);
seg1=1;
PORTD=table[num1];
seg2=0;
delay(10);
seg2=1;
PORTD=table[num2];
seg3=0;
delay(10);
seg3=1;
PORTD=table[num3];
seg4=0;
delay(10);
seg4=1;
}
void EEPROM_write(uchar wr_data,uchar wr_addr)
{
GIE=0;
EEIF=0;
EEADR=wr_addr;
EEDATA=wr_data;
EECON1bits.EEPGD=0;//
EECON1bits.CFGS=0;//
EECON1bits.WREN=1;
EECON2=0x55;
EECON2=0xaa;
EECON1bits.WR=1;
while(!EEIF);
PIR2bits.EEIF=0;
EECON1bits.WREN=0;
GIE=1;
}
uchar EEPROM_read(uchar addr)
{
uchar temp;
EEADR=addr;
EECON1bits.EEPGD=0;
EECON1bits.CFGS=0;
EECON1bits.RD=1;
// asm("NOP");
temp=EEDATA;
while(EECON1bits.RD);
return temp;
}
void forward(void)
{
static uchar j;
for (j=0; j<8; j++)
{
PORTC=coil_active[j];
delay(5);
PORTC=0xff;
delay(speed);
PORTC=coil_active[j];
delay(5);
PORTC=0xff;
delay(speed);
PORTC=coil_active[j];
delay(5);
PORTC=0xff;
delay(speed);
PORTC=coil_active[j];
delay(5);
PORTC=0xff;
delay(speed);
PORTC=coil_active[j];
delay(5);
PORTC=0xff;
delay(speed);
}
}
//*μ±¼ì2éμĸ′λDÅoÅo󣬡¡Ñóê±ò»¶Îê±¼äèñêÇ©íÑàë¾íÖ½£¬Â¶3öà′òÔ±ã2ù×÷Ô±ÄÃè¡£a£ˉ/
void hyst_forward(void)
{
static uchar j;
while(wait_t--)
{
for (j=0; j<8; j++)
{
PORTC=coil_active[j];
delay(5);
PORTC=0xff;
delay(speed);
PORTC=coil_active[j];
delay(5);
PORTC=0xff;
delay(speed);
PORTC=coil_active[j];
delay(50);
PORTC=0xff;
delay(speed);
PORTC=coil_active[j];
delay(5);
PORTC=0xff;
delay(speed);
PORTC=coil_active[j];
delay(5);
PORTC=0xff;
delay(speed);
}
}
}
void interrupt timer0()//continous check if the speed & wait_t are changed
{
uchar i,count1;
uint adval,sum=0;
if (TMR0IF==1)
{
TMR0IF=0;
TMR0ON=0;
TMR0H=(65535-1000)/256; //set the time to be 1ms;
TMR0L=(65535-1000)%256; //set the time to be 1ms;
TMR0ON=1;
count1++;
if (count1==3)
{
for (i=0;i<10;i++)//sampling 10 times to get the stable data;
{
ADCON0=0x00; //convert AN0 channel;
asm("NOP"); asm("NOP");asm("NOP");
GO=1;
while(GO);
adval=ADRESH;
sum+=adval;
speed=(int)sum/10/10;//due to dalay()routine, this needs to be divided by 10;
}
}
if (count1==6)
{
count1=0;
for (i=0;i<10;i++)//sampling 10 times to get the stable data;
{
ADCON0=0x04;//convert AN1 channel;
asm("NOP"); asm("NOP");asm("NOP");
GO=1;
while(GO);
adval=ADRESH;
sum+=adval;
wait_t=(int)sum/10/2;//upon this actual operation;
}
}
}
}
|
|