PIC Vietnam

PIC Vietnam (http://www.picvietnam.com/forum/index.php)
-   Cơ bản về vi điều khiển và PIC (http://www.picvietnam.com/forum/forumdisplay.php?f=8)
-   -   Cần Giúp Về Pic 18F4550 (http://www.picvietnam.com/forum/showthread.php?t=13301)

mongdududaydien 15-02-2012 11:24 PM

Cần Giúp Về Pic 18F4550
 
2 Attachment(s)
Mình đang học về Pic8f4550, thầy của mình có ra bài tập là sử dụng bộ timer0 để cho các led ở các chân a0, b0, c0, d0, e0 nhấp nháy với thời gian là 1s! Mình đã tham khảo nhiều trên mạng mà không hiếu sao mình vẩn không thành công, mong đc các bạn giúp đở.
Đây là code của mình:


#include "D:\DO AN DO LUONG DIEU KHIEN TU DONG\baitap3\main.h"
int16 count=0;
int8 a=0;
#int_TIMER0
void TIMER0_isr(void)
{
++count;
clear_interrupt(int_timer0);
set_timer0(56);
if(count == 25000)
{ count =0;
a=~a;
}
}

void main()
{

setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(SPI_SS_DISABLED);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
setup_timer_2(T2_DIV_BY_16,255,1);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
enable_interrupts(INT_TIMER0);
enable_interrupts(GLOBAL);
//Setup_Oscillator parameter not selected from Intr Oscillotar Config tab

// TODO: USER CODE!!
set_tris_a(0);
set_tris_b(0);
set_tris_c(0);
set_tris_d(0);
set_tris_e(0);
set_timer0(56);
while(1)
{
output_a(a);
output_b(a);
output_c(a);
output_d(a);
output_e(a);
}

}

tdm 17-02-2012 02:09 PM

Trích:

Nguyên văn bởi mongdududaydien (Post 54293)
Mình đang học về Pic8f4550, thầy của mình có ra bài tập là sử dụng bộ timer0 để cho các led ở các chân a0, b0, c0, d0, e0 nhấp nháy với thời gian là 1s! Mình đã tham khảo nhiều trên mạng mà không hiếu sao mình vẩn không thành công, mong đc các bạn giúp đở.
Đây là code của mình:


#include "D:\DO AN DO LUONG DIEU KHIEN TU DONG\baitap3\main.h"
int16 count=0;
int8 a=0;
#int_TIMER0
void TIMER0_isr(void)
{
++count;
clear_interrupt(int_timer0);
set_timer0(56);
if(count == 25000)
{ count =0;
a=~a;
}
}

void main()
{

setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(SPI_SS_DISABLED);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
setup_timer_2(T2_DIV_BY_16,255,1);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
enable_interrupts(INT_TIMER0);
enable_interrupts(GLOBAL);
//Setup_Oscillator parameter not selected from Intr Oscillotar Config tab

// TODO: USER CODE!!
set_tris_a(0);
set_tris_b(0);
set_tris_c(0);
set_tris_d(0);
set_tris_e(0);
set_timer0(56);
while(1)
{
output_a(a);
output_b(a);
output_c(a);
output_d(a);
output_e(a);
}

}

TIMER0 của pic 18f4550 có 2 chế độ 8bit/16bit.
bạn khai báo:setup_timer_0(RTCC_INTERNAL);//thì nó mặc định là timer 16bit.
theo chương trình bạn viết (giả sử thạch anh 20M) thì :
T(a đổi trạng thái)=(4/20)*(65536-56)*25000
sau khoảng thời gian đó thì các port mới thay đổi giá trị.

voxuananh 17-02-2012 04:05 PM

Bạn lưu ý 3 điểm sau:
-Config của 18f4550 phải phù hợp với giá trị thạch anh thực tế để dao động là 48MHZ
-Giá trị delay là 48M chứ ko phải 20M ( Pic này có bộ nhân tần số, con này giao tiếp USB nên mới cần tôc đọ đó)
-Giá trị cài đặt cho Timer : X= Tckl*2^(so bit)*(số lần chia) - (giá trị cần reset Timer) (micro giây)
thời gian chu kì lệnh: Tckl=4*chu kì xung=4/48 (us)


Múi giờ GMT. Hiện tại là 06:02 PM.

Tên diễn đàn: vBulletin Version 3.8.11
Được sáng lập bởi Đoàn Hiệp.
Copyright © PIC Vietnam