PIC Vietnam

Go Back   PIC Vietnam > Microchip PIC > Cơ bản về vi điều khiển và PIC

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

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

Trả lời
 
Ðiều Chỉnh Xếp Bài
Old 21-04-2011, 12:46 AM   #1
TANDEM
Đệ 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:
16.5.8 SETUP FOR PWM OPERATION
The following steps should be taken when configuring
the ECCP1 module for PWM operation:
1. Configure the PWM module:
a) Disable the ECCP1/P1A, P1B, P1C and/or
P1D outputs by setting the respective TRISD
bits.
b) Set the PWM period by loading the PR2
register.
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.
e) For Half-Bridge Output mode, set the dead-
band delay by loading the ECCP1DEL
register with the appropriate value.
2. Configure and start TMR2:
a) Clear the TMR2 interrupt flag bit by clearing
the TMR2IF bit in the PIR1 register.
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.
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.
b) Enable the ECCP1/P1A, P1B, P1C and/or
P1D pin outputs by clearing the respective
TRISD bits.
Tôi đã cố gắng đọc nhưng quả thật không thể hiểu để vận dụng chuyển thành CCS. Vậy nhờ các tiền bối giúp tôi cấu hình cho EPWM của Pic18F458 bằng CCS để sử dụng 5 kênh điều xung (C2, D4, D5, D6, D7). Xin chân thành cám ơn!
TANDEM vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
Old 25-05-2011, 03:33 PM   #2
TANDEM
Đệ 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);    
}
Sau một thời gian nghiên cứu, tôi đã thực hiện như chỉ dẫn của datasheet. Tuy nhiên, kết qua thi không như mong muốn, trong đoạn code trên nếu set_pwm1_duty(50); set_pwm2_duty(50); thì 2 pin D4(P1A) và D7(P1D) có tín hiệu, nếu set_pwm1_duty(50); set_pwm2_duty(50); set_pwm3_duty(50); set_pwm4_duty(50); thì chương trình sẽ báo lỗi.
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.
TANDEM vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
Old 26-05-2011, 10:09 AM   #3
shakespeare_a18
Đệ 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.
shakespeare_a18 vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
Trả lời


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à 05:13 PM.


Đượ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