برنامه ای برای کم نوری به پرنوری و عکس آن . . .
غیر ممکنه ...
من همین الان دارم توی پروتئوس میبینم که داره کار میکنه ...
دقیقا همین کد رو کپی بگیر و توی کدویژن paste کن
و روی Build all project files کلیک کن
[code=c]
#include <tiny13a.h>
#include <delay.h>
#define Set_LED1 PORTB.0 = 1
#define Set_LED2 PORTB.1 = 1
#define Set_LED3 PORTB.2 = 1
#define Set_LED4 PORTB.3 = 1
#define Set_LED5 PORTB.4 = 1
#define max_var_PWM 50
unsigned char a = max_var_PWM , pwm[10];
// Timer 0 output compare A interrupt service routine
interrupt [TIM0_COMPA] void timer0_compa_isr(void)
{
if(a)
{
if(a == pwm[1])Set_LED1;
if(a == pwm[2])Set_LED2;
if(a == pwm[3])Set_LED3;
if(a == pwm[4])Set_LED4;
if(a == pwm[5])Set_LED5;
}
if(--a == 255)
{
a = max_var_PWM;
PORTB=0;
}
}
void main(void)
{
signed char x;
// Crystal Oscillator division factor: 1
CLKPR=0x80;
CLKPR=0x00;
PORTB=0x00;
DDRB=0x1F;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 1200 kHz
// Mode: CTC top=OCR0A
// OC0A output: Disconnected
// OC0B output: Disconnected
TCCR0A=0x02;
TCCR0B=0x02;
OCR0A=94;
// Timer/Counter 0 Interrupt(s) initialization
TIMSK0=0x04;
// Global enable interrupts
#asm("sei"
while (1)
{
for(x=0;x<51;x++)
{
pwm[1]=x;
delay_ms(10);
}
for(x=50;x>=0;x--)
{
pwm[1]=x;
delay_ms(10);
}
}
}
[/code]
فکر کنم فهمیدم مشکلت چیه ...
مشکلتون اینه که متغیر x رو از نوع unsigned char تعریف کردید ...
من گفتم که متغیر x رو باید از نوع با علامت signed char تعریف کنید ...
معلومه که برنامه نویسی رو تازه شروع کردیاااااا ...
بیشتر دقت کن
یه نکته دیگه
شما که از ترانزیستور استفاده میکنید بهم میگفتید تا بگم چیکار کنید که اگر به pwm های مورد نظر عدد کم بدهیم کم نور و زیاد بدهیم پرنورتر شود ...
برای این منظور باید صفرها را یک و یک ها را صفر کنید ...
مانند کد زیر :
[code=c]
#include <tiny13a.h>
#include <delay.h>
#define Set_LED1 PORTB.0 = 0
#define Set_LED2 PORTB.1 = 0
#define Set_LED3 PORTB.2 = 0
#define Set_LED4 PORTB.3 = 0
#define Set_LED5 PORTB.4 = 0
#define max_var_PWM 50
unsigned char a = max_var_PWM , pwm[10];
// Timer 0 output compare A interrupt service routine
interrupt [TIM0_COMPA] void timer0_compa_isr(void)
{
if(a)
{
if(a == pwm[1])Set_LED1;
if(a == pwm[2])Set_LED2;
if(a == pwm[3])Set_LED3;
if(a == pwm[4])Set_LED4;
if(a == pwm[5])Set_LED5;
}
if(--a == 255)
{
a = max_var_PWM;
PORTB=31;
}
}
void main(void)
{
signed char x;
// Crystal Oscillator division factor: 1
CLKPR=0x80;
CLKPR=0x00;
PORTB=0x00;
DDRB=0x1F;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 1200 kHz
// Mode: CTC top=OCR0A
// OC0A output: Disconnected
// OC0B output: Disconnected
TCCR0A=0x02;
TCCR0B=0x02;
OCR0A=94;
// Timer/Counter 0 Interrupt(s) initialization
TIMSK0=0x04;
// Global enable interrupts
#asm("sei"
while (1)
{
for(x=0;x<51;x++)
{
pwm[1]=x;
delay_ms(10);
}
for(x=50;x>=0;x--)
{
pwm[1]=x;
delay_ms(10);
}
}
}
[/code]
موفق باشید
نوشته اصلی توسط b.saeed65
من همین الان دارم توی پروتئوس میبینم که داره کار میکنه ...
دقیقا همین کد رو کپی بگیر و توی کدویژن paste کن
و روی Build all project files کلیک کن
[code=c]
#include <tiny13a.h>
#include <delay.h>
#define Set_LED1 PORTB.0 = 1
#define Set_LED2 PORTB.1 = 1
#define Set_LED3 PORTB.2 = 1
#define Set_LED4 PORTB.3 = 1
#define Set_LED5 PORTB.4 = 1
#define max_var_PWM 50
unsigned char a = max_var_PWM , pwm[10];
// Timer 0 output compare A interrupt service routine
interrupt [TIM0_COMPA] void timer0_compa_isr(void)
{
if(a)
{
if(a == pwm[1])Set_LED1;
if(a == pwm[2])Set_LED2;
if(a == pwm[3])Set_LED3;
if(a == pwm[4])Set_LED4;
if(a == pwm[5])Set_LED5;
}
if(--a == 255)
{
a = max_var_PWM;
PORTB=0;
}
}
void main(void)
{
signed char x;
// Crystal Oscillator division factor: 1
CLKPR=0x80;
CLKPR=0x00;
PORTB=0x00;
DDRB=0x1F;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 1200 kHz
// Mode: CTC top=OCR0A
// OC0A output: Disconnected
// OC0B output: Disconnected
TCCR0A=0x02;
TCCR0B=0x02;
OCR0A=94;
// Timer/Counter 0 Interrupt(s) initialization
TIMSK0=0x04;
// Global enable interrupts
#asm("sei"
while (1)
{
for(x=0;x<51;x++)
{
pwm[1]=x;
delay_ms(10);
}
for(x=50;x>=0;x--)
{
pwm[1]=x;
delay_ms(10);
}
}
}
[/code]
فکر کنم فهمیدم مشکلت چیه ...
مشکلتون اینه که متغیر x رو از نوع unsigned char تعریف کردید ...
من گفتم که متغیر x رو باید از نوع با علامت signed char تعریف کنید ...
معلومه که برنامه نویسی رو تازه شروع کردیاااااا ...
بیشتر دقت کن
یه نکته دیگه
شما که از ترانزیستور استفاده میکنید بهم میگفتید تا بگم چیکار کنید که اگر به pwm های مورد نظر عدد کم بدهیم کم نور و زیاد بدهیم پرنورتر شود ...
برای این منظور باید صفرها را یک و یک ها را صفر کنید ...
مانند کد زیر :
[code=c]
#include <tiny13a.h>
#include <delay.h>
#define Set_LED1 PORTB.0 = 0
#define Set_LED2 PORTB.1 = 0
#define Set_LED3 PORTB.2 = 0
#define Set_LED4 PORTB.3 = 0
#define Set_LED5 PORTB.4 = 0
#define max_var_PWM 50
unsigned char a = max_var_PWM , pwm[10];
// Timer 0 output compare A interrupt service routine
interrupt [TIM0_COMPA] void timer0_compa_isr(void)
{
if(a)
{
if(a == pwm[1])Set_LED1;
if(a == pwm[2])Set_LED2;
if(a == pwm[3])Set_LED3;
if(a == pwm[4])Set_LED4;
if(a == pwm[5])Set_LED5;
}
if(--a == 255)
{
a = max_var_PWM;
PORTB=31;
}
}
void main(void)
{
signed char x;
// Crystal Oscillator division factor: 1
CLKPR=0x80;
CLKPR=0x00;
PORTB=0x00;
DDRB=0x1F;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 1200 kHz
// Mode: CTC top=OCR0A
// OC0A output: Disconnected
// OC0B output: Disconnected
TCCR0A=0x02;
TCCR0B=0x02;
OCR0A=94;
// Timer/Counter 0 Interrupt(s) initialization
TIMSK0=0x04;
// Global enable interrupts
#asm("sei"
while (1)
{
for(x=0;x<51;x++)
{
pwm[1]=x;
delay_ms(10);
}
for(x=50;x>=0;x--)
{
pwm[1]=x;
delay_ms(10);
}
}
}
[/code]
موفق باشید
دیدگاه