PIC Vietnam

PIC Vietnam (http://www.picvietnam.com/forum/index.php)
-   Các ngôn ngữ lập trình khác (CCS C, HT PIC,...) (http://www.picvietnam.com/forum/forumdisplay.php?f=12)
-   -   Ai sữa giùm em chương trình viết trên MikroC này với!!! (http://www.picvietnam.com/forum/showthread.php?t=5586)

bocon1903 22-12-2009 05:06 PM

Ai sữa giùm em chương trình viết trên MikroC này với!!!
 
chương trình này em viết trên MikroC 8.2
Code:

void main() {
  PORTA = 0;                // Initialize PORTA
  TRISA = 0;                // Configure PORTA as output
  PORTB = 0;                // Initialize PORTB
  TRISB = 0;                // Configure PORTB as output
  PORTC = 0;                // Initialize PORTC
  TRISC = 0;                // Configure PORTC as output
  PORTD = 0;                // Initialize PORTD
  TRISD = 0;                // Configure PORTD as output
  while(1) {
    PORTA =  ~PORTA;        // toggle PORTA
    Delay_ms(1000);          // one second delay
    PORTB =  ~PORTB;        // toggle PORTB
    Delay_ms(1000);          // one second delay
    PORTC =  ~PORTC;        // toggle PORTC
    Delay_ms(1000);          // one second delay
    PORTD =  ~PORTD;        // toggle PORTD
    Delay_ms(1000);          // one second delay
  }
}

các port B,C,D của em đều chạy đúng,chỉ có mỗi port a chỉ chạy chân RA4,còn các RA khác không thay đổi(các port em nối với led). em mới học,có gì thiếu sót xin các anh chỉ giáo.em cám ơn

nghia_tdh21 22-12-2009 05:45 PM

Bạn chưa config cho portA, ko biết bạn dùng pic gì, bạn phải mắc thêm một điện trở kéo lên ở ra4. Chương trình của bạn mình thừ dùng cho pic16f877a sửa lại như sau:
Code:

void main() {
  ADCON1=0x06;          //config porta thanh digital in/out
  PORTA = 0;                // Initialize PORTA
  TRISA = 0;                // Configure PORTA as output
  PORTB = 0;                // Initialize PORTB
  TRISB = 0;                // Configure PORTB as output
  PORTC = 0;                // Initialize PORTC
  TRISC = 0;                // Configure PORTC as output
  PORTD = 0;                // Initialize PORTD
  TRISD = 0;                // Configure PORTD as output
  while(1) {
    PORTA =  ~PORTA;        // toggle PORTA
    Delay_ms(1000);          // one second delay
    PORTB =  ~PORTB;        // toggle PORTB
    Delay_ms(1000);          // one second delay
    PORTC =  ~PORTC;        // toggle PORTC
    Delay_ms(1000);          // one second delay
    PORTD =  ~PORTD;        // toggle PORTD
    Delay_ms(1000);          // one second delay
  }
}


bocon1903 22-12-2009 07:09 PM

cám ơn anh,em dùng con 877a,nó chạy tốt rồi.

bocon1903 22-12-2009 10:02 PM

Code:

#include "Display_utils.h"

unsigned short i;

void main() {
  ADCON1=0x06;
  PORTA  = 0;
  TRISA  = 0;
  PORTD  = 0;
  TRISD  = 0;
  do {
    for (i = 0; i <= 9; i++) {
      PORTD = ~mask(i);          // bring appropriate value to PORTD
      PORTA = 1;                // turn on appropriate 7seg. display
      Delay_ms(1000);
    }
  } while (1);                  //endless loop
}

hàm mask(i) là một chương trình con chứa trong Display_utils.h do em viết và không hề tác động lên portA,nhưng không hiểu sao chân RA0 vẫn ở mức thấp dù em đã set portA lên 1.


Múi giờ GMT. Hiện tại là 04:25 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