PIC Vietnam

Go Back   PIC Vietnam > Các Đề Tài > PIC Việt Nam - Chuyên nghiệp

Tài trợ cho PIC Vietnam
Trang chủ Đăng Kí Hỏi/Ðáp Thành Viên Lịch Bài Trong Ngày Vi điều khiển

PIC Việt Nam - Chuyên nghiệp Chúng ta sẽ thảo luận ở đây nhưng vấn đề về phương pháp nghiên cứu, cách làm việc và tổ chức công việc...

 
 
Ðiều Chỉnh Xếp Bài
Prev Previous Post   Next Post Next
Old 07-12-2011, 12:28 AM   #1
phamvanduan
Nhập môn đệ tử
 
Tham gia ngày: Aug 2010
Bài gửi: 3
:
Unhappy giúp em với.code pic 18f4431

Chào các anh chi.em đang viết code cho inverter nhưng mà khi chạy mô phỏng thì không hiểu sao chay không dúng.mong anh chi giúp đỡ
#include "inverter.h"
#bit led0 = PORTD.0
#bit led1 = PORTD.1
#bit led2 = PORTD.2
#bit led3 = PORTD.3
#bit led4 = PORTD.4
#bit led5 = PORTD.5
#bit led6 = PORTD.6
#bit led7 = PORTD.7
#bit SD1 = PORTC.3
#bit SD2 = PORTC.0
#bit ACline = PORTC.4
#define inverter 0
#define change 1
int1 negative = 1,a,j=0,mode;
unsigned char del=0,sine_temp,Gain_k,duty;
unsigned int16 V_out,V_bat,I_bat,I_change;
char i;

unsigned char sine_table[200]={0, 4, 8, 12, 16,20, 24, 27, 31, 35, 39, 43, 47, 51,
55, 58, 62, 66, 70, 74, 77, 81, 85, 88, 92, 96, 99,
103, 106, 110, 114, 117, 120, 124, 127, 131, 134, 137,
141, 144, 147, 150, 153, 156, 159, 162, 165, 168, 171,
174, 177, 180, 182, 185, 188, 190, 193, 195, 198, 200,
202, 205, 207, 209, 211, 213, 215, 217, 219, 221, 223,
225, 226, 228, 229, 231, 232, 234, 235, 237, 238, 239,
240, 241, 242, 243, 244, 245, 246, 246, 247, 248, 248,
248, 249, 249, 250, 250, 250, 250, 250, 250, 250, 250,
250, 249, 249, 248, 248, 248, 247, 246, 246, 245, 244,
243, 242, 241, 240, 239, 238, 237, 235, 234, 232, 231,
230, 228, 226, 225, 223, 221, 219, 217, 215, 213, 211,
209, 207, 205, 202, 200, 198, 195, 193, 190, 188, 185,
182, 180, 177, 174, 171, 168, 165, 162, 159, 156, 153,
150, 150, 144, 141, 137, 134, 131, 127, 124, 120, 117,
114, 110, 106, 103, 99, 96, 92, 88, 85, 81, 77, 74,
70, 66, 62, 58, 55, 51, 47, 43, 39, 35, 31, 27, 24,
20, 16, 12, 8, 4};
#int_TIMER1
void TIMER1_isr(void)
{
a = 1; //moi 10ms cho phep thay doi do rong xung
set_timer1(15535); //ngat timer1 10ms
}

#int_TIMER2
void TIMER2_isr(void) //xuat xung PWM tan so 20khz
{
if (mode == inverter)
{
if(negative == 1)
{
set_pwm1_duty(sine_table[del]);
set_pwm2_duty(0);
j = 1;
led6 = 1;
//PORTD = 0x01;
}
else
{
if(j==1)
{
j = 0;
delay_us(5);
set_pwm1_duty(0);
set_pwm2_duty(sine_table[del]);
led7 = 1;
//PORTD = 0x02;
}
else
{
set_pwm1_duty(0);
set_pwm2_duty(sine_table[del]);
led7 = 1;
//PORTD = 0x02;
}
}
if(++del > 200)
{
del = 0;
negative = ~negative;
}
}
else if(mode == change)
{
set_pwm1_duty(duty);
}
}

void inverter_program();
void change_program();

void main()
{ //int8 del=0;
set_tris_C(0xf0);
set_tris_D(0x00);
set_tris_A(0xff);
set_tris_B(0xff);
set_tris_E(0xff);
setup_adc_ports(sAN0|sAN1|sAN2|sAN3|sAN6|VSS_VDD);
setup_adc(ADC_CLOCK_INTERNAL);
//setup_timer_0(RTCC_INTERNAL);

setup_ccp1(CCP_PWM);
setup_ccp2(CCP_PWM);
// PORTD = 0xff;
while(true)
{
if(ACline == 1)
{
mode = change;
led0 = 1;
led1 = 0;
}
else
{
mode = inverter;
led1 = 1;
led0 = 0;
}
switch (mode)
{
case inverter : inverter_program();
break;
case change : change_program();
break;
}

}

}
void inverter_program(void)
{
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
setup_timer_2(T2_DIV_BY_1,249,1);
enable_interrupts(INT_TIMER1);
enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL);
SD2 = 1;
set_adc_channel(0);
V_out = read_adc();
delay_us(20);
set_adc_channel(1);
V_bat = read_adc();
delay_us(20);
set_adc_channel(2);
I_bat = read_adc();
delay_us(20);
if((V_bat<590)||(I_bat>303))
{
led2 = 1;
SD1 = 1;
}
else if((V_bat>590)&&(I_bat<303))
{
led2 = 0;
SD1 = 0;
}
if (V_out<522)
{
for(;(V_out<522)|(Gain_k>=100);Gain_k++) // neu dien ap < 220V tang do rong xung
{
if (a)
{ a=0;
for(i=0; i<200; i++)
{
sine_temp=(long)sine_table[i]-100; // thay doi gia tri trong bang sin
sine_temp=(long)(sine_temp*Gain_k)/100;
sine_table[i]=(unsigned char)(sine_temp+100);
}
}
}
}
else
{
for(;(V_out<=522)|(Gain_k<=100);Gain_k--) // neu dien ap > 220V giam do rong xung
{
if (a)
{ a=0;
for(i=0; i<200; i++)
{
sine_temp=(long)sine_table[i]-100; // thay doi gia tri trong bang sin
sine_temp=(long)(sine_temp*Gain_k)/100;
sine_table[i]=(unsigned char)(sine_temp+100);
}
}
}
}

}
void change_program(void)
{
setup_timer_2(T2_DIV_BY_1,249,1);
enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL);
SD1 = 1;
SD2 = 0;
set_adc_channel(1);
V_bat = read_adc();
delay_us(20);
set_adc_channel(6);
I_change = read_adc();
delay_us(20);
if(V_bat<=588)
{
led3 = 1;
duty = 65;
}
else if (V_bat<835)
{
led4 = 1;
duty = 50;
}

}


#include <18F4431.h>
#device adc=10

#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES HS //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOIESO //Internal External Switch Over mode disabled
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOPUT //No Power Up Timer
#FUSES NOCPD //No EE protection
#FUSES NOSTVREN //Stack full/underflow will not cause reset
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOWRT //Program memory not write protected
#FUSES NOWRTD //Data EEPROM not write protected
#FUSES NOFCMEN //Fail-safe clock monitor disabled
#FUSES NOWINEN //WDT Timer Window Disabled
#FUSES T1LOWPOWER //Timer1 low power operation when in sleep
#FUSES HPOL_HIGH //High-Side Transistors Polarity is Active-High (PWM 1,3,5 and 7)
//PWM module high side output pins have active high output polarity
#FUSES NOWRTC //configuration not registers write protected
#FUSES NOWRTB //Boot block not write protected
#FUSES NOEBTR //Memory not protected from table reads
#FUSES NOEBTRB //Boot block not protected from table reads
#FUSES NOCPB //No Boot Block code protection

#use delay(clock=20000000)
#byte PORTA = 0xF80
#byte PORTB = 0xF81
#byte PORTC = 0xF82
#byte PORTD = 0xF83
#byte PORTE = 0xF84


cảm ơn anh chị nhiều
phamvanduan vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
 


Quyền Sử Dụng Ở Diễn Ðàn
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is Mở
Smilies đang Mở
[IMG] đang Mở
HTML đang Tắt

Chuyển đến


Múi giờ GMT. Hiện tại là 03:25 AM.


Được sáng lập bởi Đoàn Hiệp
Powered by vBulletin®
Page copy protected against web site content infringement by Copyscape
Copyright © PIC Vietnam