Prerusenie od Timer0 v jazyku C
Moderátor: Moderátoři
Prerusenie od Timer0 v jazyku C
Zdravim nevie niekto ako sa nastavuje prerusenie casovaca0 a najma ako sa povoluje v jazyku c.
Tuto je program:
#include <C:\WinAVR-20100110\avr\include\avr\io.h>
#include <C:\WinAVR-20100110\avr\include\avr\interrupt.h>
#define vpredButton_PRESSED !(PIND & 0x01) //PD0 vpred
#define vpredButton_OPEN (PIND & 0x01)
#define vzadButton_PRESSED !(PIND & 0x02) //PD1 vzad
#define vzadButton_OPEN (PIND & 0x02)
#define vlavoButton_PRESSED !(PIND & 0x04) //PD2 vlavo
#define vlavoButton_OPEN (PIND & 0x04)
#define vpravoButton_PRESSED !(PIND & 0x08) //PD3 vpravo
#define vpravoButton_OPEN (PIND & 0x08)
void timer0_init(void)
{
TCNT0=131;
TCCR0 = 0x81; // TCnt=256-125f=64000 -> 8MHz/256
TIMSK=0x01;//povol.
}
void port_init(void)
{
PORTD = 0x00; //PD0-PD3 ovladanie vstupy
DDRD = 0x00; //PD4-OC1B, PD5-OC1A motor c.1 PD7-OC2 motor c.2vystupy
/*PORTB = 0x00; //motor c2
DDRB = 0x08; //PB3- OC0 vystup*/
PORTC = 0xFF;
DDRC = 0xFF;
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void init_devices(void)
{
port_init();
timer0_init();
MCUCR = 0x0;
GICR =0x00;
GIFR =0x00;
SFIOR=0x00;
WDTCR=0x00;
SREG=0x00;
}
//---------------------------------------------------------------------------
void delay_ms(int miliSec) //1 Mhz crystal
{
int i,j;
for(i=0;i<miliSec;i++)
for(j=0;j<100;j++)
{
asm("nop");
asm("nop");
}
}
//************************ main ***************************
//---------------------------------------------------------------------------
volatile int PWM_cnt=0;
int PWM_T=64;
int PWM_t0=-32;
int PWM_t1=50;
//int PORTC=0;
const int _PWM0=1;
const int _PWM1=2;
const int _PWM2=4;
const int _PWM3=8;
//---------------------------------------------------------------------------
void timer0_oweflow(void)
{
TCNT0=131;
if (PWM_cnt==0)
{
PORTC|=_PWM0|_PWM1|_PWM2|_PWM3;
if (PWM_t0>=0) PORTC^=_PWM0;
if (PWM_t0<=0) PORTC^=_PWM1;
if (PWM_t1>=0) PORTC^=_PWM2;
if (PWM_t1<=0) PORTC^=_PWM3;
}
PWM_cnt++;
if (PWM_cnt==-PWM_t0) PORTC^=_PWM0;
if (PWM_cnt==+PWM_t0) PORTC^=_PWM1;
if (PWM_cnt==-PWM_t1) PORTC^=_PWM2;
if (PWM_cnt==+PWM_t1) PORTC^=_PWM3;
if (PWM_cnt>=PWM_T)
{
PWM_cnt=0;
}
}
//---------------------------------------------------------------------------
int main(void)
{
init_devices();
for(;;)
{
//---------------------------------------------------------------------------
if(vpredButton_PRESSED)
{
delay_ms(500);
if(vpredButton_OPEN)
{
delay_ms(500);
}
if(PWM_t0<PWM_T) PWM_t0++;
if(PWM_t1<PWM_T) PWM_t1++;
}
}
//---------------------------------------------------------------------------
if(vzadButton_PRESSED)
{
delay_ms(100);
if(vzadButton_OPEN) {
delay_ms(100);
}
if(PWM_t0>-PWM_T) PWM_t0--;
if(PWM_t1>-PWM_T) PWM_t1--;
}
//---------------------------------------------------------------------------
if(vlavoButton_PRESSED)
{
delay_ms(500);
if(vpredButton_OPEN)
{
delay_ms(500);
}
if(PWM_t0<PWM_T) PWM_t0++;
if(PWM_t1<PWM_T) PWM_t1--;
}
//---------------------------------------------------------------------------
if(vlavoButton_PRESSED)
{
delay_ms(100);
if(vzadButton_OPEN) {
delay_ms(100);
}
if(PWM_t0>-PWM_T) PWM_t0--;
if(PWM_t1>-PWM_T) PWM_t1++;
};
}
//****************************** END ***************************************
Program generuje PWM po zatlaceni urciteho tlacidla (Port D 0-3), PWM je na (Port C 0-3), po preteceni casovaca 0 sa ma vyvolat podprogram a vlastne neviem nastavit ten casovac- prerusenie.
Proste chcem ovladat dva jednosmerne motorceky pomocov PWM, tak aby isli stale 2 PWM sucastne
#include <C:\WinAVR-20100110\avr\include\avr\io.h>
#include <C:\WinAVR-20100110\avr\include\avr\interrupt.h>
#define vpredButton_PRESSED !(PIND & 0x01) //PD0 vpred
#define vpredButton_OPEN (PIND & 0x01)
#define vzadButton_PRESSED !(PIND & 0x02) //PD1 vzad
#define vzadButton_OPEN (PIND & 0x02)
#define vlavoButton_PRESSED !(PIND & 0x04) //PD2 vlavo
#define vlavoButton_OPEN (PIND & 0x04)
#define vpravoButton_PRESSED !(PIND & 0x08) //PD3 vpravo
#define vpravoButton_OPEN (PIND & 0x08)
void timer0_init(void)
{
TCNT0=131;
TCCR0 = 0x81; // TCnt=256-125f=64000 -> 8MHz/256
TIMSK=0x01;//povol.
}
void port_init(void)
{
PORTD = 0x00; //PD0-PD3 ovladanie vstupy
DDRD = 0x00; //PD4-OC1B, PD5-OC1A motor c.1 PD7-OC2 motor c.2vystupy
/*PORTB = 0x00; //motor c2
DDRB = 0x08; //PB3- OC0 vystup*/
PORTC = 0xFF;
DDRC = 0xFF;
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void init_devices(void)
{
port_init();
timer0_init();
MCUCR = 0x0;
GICR =0x00;
GIFR =0x00;
SFIOR=0x00;
WDTCR=0x00;
SREG=0x00;
}
//---------------------------------------------------------------------------
void delay_ms(int miliSec) //1 Mhz crystal
{
int i,j;
for(i=0;i<miliSec;i++)
for(j=0;j<100;j++)
{
asm("nop");
asm("nop");
}
}
//************************ main ***************************
//---------------------------------------------------------------------------
volatile int PWM_cnt=0;
int PWM_T=64;
int PWM_t0=-32;
int PWM_t1=50;
//int PORTC=0;
const int _PWM0=1;
const int _PWM1=2;
const int _PWM2=4;
const int _PWM3=8;
//---------------------------------------------------------------------------
void timer0_oweflow(void)
{
TCNT0=131;
if (PWM_cnt==0)
{
PORTC|=_PWM0|_PWM1|_PWM2|_PWM3;
if (PWM_t0>=0) PORTC^=_PWM0;
if (PWM_t0<=0) PORTC^=_PWM1;
if (PWM_t1>=0) PORTC^=_PWM2;
if (PWM_t1<=0) PORTC^=_PWM3;
}
PWM_cnt++;
if (PWM_cnt==-PWM_t0) PORTC^=_PWM0;
if (PWM_cnt==+PWM_t0) PORTC^=_PWM1;
if (PWM_cnt==-PWM_t1) PORTC^=_PWM2;
if (PWM_cnt==+PWM_t1) PORTC^=_PWM3;
if (PWM_cnt>=PWM_T)
{
PWM_cnt=0;
}
}
//---------------------------------------------------------------------------
int main(void)
{
init_devices();
for(;;)
{
//---------------------------------------------------------------------------
if(vpredButton_PRESSED)
{
delay_ms(500);
if(vpredButton_OPEN)
{
delay_ms(500);
}
if(PWM_t0<PWM_T) PWM_t0++;
if(PWM_t1<PWM_T) PWM_t1++;
}
}
//---------------------------------------------------------------------------
if(vzadButton_PRESSED)
{
delay_ms(100);
if(vzadButton_OPEN) {
delay_ms(100);
}
if(PWM_t0>-PWM_T) PWM_t0--;
if(PWM_t1>-PWM_T) PWM_t1--;
}
//---------------------------------------------------------------------------
if(vlavoButton_PRESSED)
{
delay_ms(500);
if(vpredButton_OPEN)
{
delay_ms(500);
}
if(PWM_t0<PWM_T) PWM_t0++;
if(PWM_t1<PWM_T) PWM_t1--;
}
//---------------------------------------------------------------------------
if(vlavoButton_PRESSED)
{
delay_ms(100);
if(vzadButton_OPEN) {
delay_ms(100);
}
if(PWM_t0>-PWM_T) PWM_t0--;
if(PWM_t1>-PWM_T) PWM_t1++;
};
}
//****************************** END ***************************************
Program generuje PWM po zatlaceni urciteho tlacidla (Port D 0-3), PWM je na (Port C 0-3), po preteceni casovaca 0 sa ma vyvolat podprogram a vlastne neviem nastavit ten casovac- prerusenie.
Proste chcem ovladat dva jednosmerne motorceky pomocov PWM, tak aby isli stale 2 PWM sucastne