View Single Post
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