![]() |
|
Tài trợ cho PIC Vietnam |
||||||||
| Cơ bản về vi điều khiển và PIC Những bài hướng dẫn cơ bản nhất để làm quen với vi điều khiển PIC |
![]() |
|
|
Ðiều Chỉnh | Xếp Bài |
|
|
#1 | |
|
Đệ tử 2 túi
Tham gia ngày: Aug 2008
Bài gửi: 40
: |
Cấu hình EPWM của Pic18F458 bằng CCS
Trích:
|
|
|
|
|
|
|
#2 |
|
Đệ tử 2 túi
Tham gia ngày: Aug 2008
Bài gửi: 40
: |
Code:
//********************************************************************************************
//Header:
//File Name:
//Hard: PIC18F458
//Author:
//Date: ... April 2011
//********************************************************************************************
#include <18F458.H>
#FUSES NOWDT //No Watch Dog Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOPUT //No power Up Timer
#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 HS //
#use delay(clock=40000000) //Thach anh gan tren board la 40MHz
#byte porta = 0xF80
#byte portb = 0xF81
#byte portc = 0xF82
#byte portd = 0xF83
#byte porte = 0xF84
#byte PR2 = 0xFCB
#byte ECCP1CON = 0xFBA
#byte ECCPR1L = 0xFBB
#byte ECCPR1H = 0xFBC
#byte PIR1 = 0xF9E
#byte T2CON = 0xFCA
void khoitao(void);
void setup_ECCP(void);
void main(void)
{
khoitao();
}
//Ham khoi tao**************************************************************************
void khoitao(void)
{
setup_adc_ports(NO_ANALOGS); //tat ca porta la digital
setup_ECCP();
set_pwm1_duty(50);
set_pwm2_duty(50);
//set_pwm3_duty(50);
//set_pwm4_duty(50);
}
//Ham phuong an*************************************************************************
void setup_ECCP(void) //cau hinh ECCP de su dung 4 kenh(d4-d7)PWM o che do "single output"
{
//1. Configure the PWM module:
//a) Disable the ECCP1/P1A, P1B, P1C and/or P1D outputs by setting the respective TRISD bits.
set_tris_d(0xFF);
//b) Set the PWM period by loading the PR2 register
PR2 = 50;
//c) Set the PWM duty cycle by loading the ECCPR1L register and ECCP1CON<5:4> bits.
//d) Configure the ECCP1 module for the desired PWM operation by loading the ECCP1CON
//register with the appropriate value. With the ECCP1M<3:0> bits, select
//the active-high/low levels for each PWM output. With the EPWM1M<1:0> bits, select
//one of the available output modes.
ECCPR1L = 0x00;
ECCP1CON = 0b00001100; //Chon che do "single output" va cac "pins out" deu o muc "high"
//2. Configure and start TMR2:
//a) Clear the TMR2 interrupt flag bit by clearing the TMR2IF bit in the PIR1 register.
PIR1 = 0x00;
//b) Set the TMR2 prescale value by loading the T2CKPS bits (T2CON<1:0>).
//c) Enable Timer2 by setting the TMR2ON bit (T2CON<2>) register.
T2CON = 0x00;
T2CON |= 0x04;
//3. Enable PWM outputs after a new cycle has started:
//a) Wait until TMR2 overflows (TMR2IF bit becomes a ‘1’). The new PWM cycle begins here.
while (PIR1)
{
}
//b) Enable the ECCP1/P1A, P1B, P1C and/or P1D pin outputs by clearing the respective TRISD bits.
set_tris_d(0x0F);
}
Vậy nhờ các tiền bối phát hiện giúp lỗi ở đâu. Xin cảm ơn! thay đổi nội dung bởi: TANDEM, 25-05-2011 lúc 03:39 PM. |
|
|
|
|
|
#3 |
|
Đệ tử 1 túi
Tham gia ngày: Aug 2010
Nơi Cư Ngụ: TP HCM
Bài gửi: 22
: |
Thấy code của bạn có vẻ rắc rối quá.
Tôi xài 18F4550. chế độ Full bridge: setup_ccp1(0xCC); chế độ Full bridge rev (quay ngược): setup_ccp1(0x4C); bên cạnh đó cần set tris d là output. |
|
|
|
![]() |
| Ðiều Chỉnh | |
| Xếp Bài | |
|
|