|
|
View previous topic :: View next topic |
Author |
Message |
Ttelmah
Joined: 11 Mar 2010 Posts: 19590
|
|
Posted: Mon Mar 14, 2016 8:03 am |
|
|
Look at the code I pointed to.
You can't have the PWM in your main code. It needs to be done by a timer (tick) interrupt, if your main code is going to be able to do anything without interfering with the PWM. |
|
|
baris_akinci
Joined: 10 Mar 2016 Posts: 17 Location: Ankara
|
pwm and normal output on same pin for 16f628a |
Posted: Mon Mar 21, 2016 2:05 pm |
|
|
With your advice, I tried to generate pwm by using timer1. Please check the following. Actually, this code works well in Proteus but in real, this does not work. I mean I can not control the pic and I just see LED flash once, then nothing!!
In order to be seen more clear, I deleted codes, typed sub-program instead.
Code: | #include <16F628A.h>
#FUSES INTRC_IO, NOWDT, NOPUT, NOMCLR, NOBROWNOUT, NOLVP
#use fast_io(a)
#use fast_io(b)
#use delay(internal=4000000) // dahili osilatör ayarları
#use rs232(baud=2400,xmit=PIN_B2,rcv=PIN_B1,ERRORS) // RF haberleşme ayarları
#priority rda,ext,timer1 // rda yani haberleşme kesmesi önceliği, harici kesme ikinci öncelikte
#define LED1 pin_a0
#define LED2 pin_a1
#define LED3 pin_a2
#define LED4 pin_a3
#define KONTROL pin_b5
char c[10]; // senkronizasyon kontrolü dizini
char kapat[3]; // acma kapama dizini
int counter=1; // acma kapama değişkeni
int durum=0; // senkronizasyon sağlandımı değişkeni
int index = 0; // senkronizasyon sağlandımı dizini
int inc=0; // simulasyon dallanma değişkeni
int red_duty=63;
int Intcount=0;
#int_rda
void RDA_isr(void)
{
// rda sub-program
}
#int_timer1
void timer_pwm_isr(void)
{
set_timer1(65400);
if (Intcount < 255)
{
if (Intcount == red_duty)
{
output_low(LED1); output_low(LED2);output_low(LED3);output_low(LED4);
}
}
else if(Intcount == 255)
{
Intcount = 0;
output_high(LED1); output_high(LED2);output_high(LED3);output_high(LED4);
}
Intcount++;
}
#INT_EXT
void EXT_isr(void) // harici buton ile simulasyon değişimi yapılması fonksiyonu
{
// ext sub-program
}
void sim1() {
// simulation 1 sub-program
}
void sim2() {
// simulation 2 sub-program
}
void sim3() {
// simulation 3 sub-program
}
void sim4() {
// simulation 4 sub-program
}
void sim5() {
// simulation 5 sub-program
}
void main()
{
disable_interrupts(int_timer1);
enable_interrupts(int_rda); // rda kesmesi aktif ediliyor
enable_interrupts(INT_EXT); // harici kesme aktif ediliyor
enable_interrupts(GLOBAL); // rda kesmesinin geleceği bildiriliyor
set_timer1(65400);
set_tris_a(0b00010000);
set_tris_b(0b00000011);
while(TRUE)
{
if(inc==1) {
disable_interrupts(INT_TIMER1);
while(inc==1) {sim1();}
enable_interrupts(int_rda);
}
else if(inc==2) {
disable_interrupts(INT_TIMER1);
while (inc==2){sim2();}
enable_interrupts(int_rda);
}
else if(inc==3) {
disable_interrupts(INT_TIMER1);
while (inc==3){sim3();}
enable_interrupts(int_rda);
}
else if(inc==4) {
disable_interrupts(INT_TIMER1);
while (inc==4){sim4();}
enable_interrupts(int_rda);
}
else if(inc==5) {
while(inc==5) {
setup_timer_1(T1_INTERNAL | T1_DIV_BY_1);
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL); }
enable_interrupts(int_rda);
}
else if(inc==6) inc=1;
else if(inc==0) inc=5;
else if(inc==15) { disable_interrupts(INT_TIMER1);
output_low(LED1);output_low(LED2);output_low(LED3);output_low(LED4);
}
else inc=inc;
}
}
|
|
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|