PIC Vietnam

Go Back   PIC Vietnam > Microchip PIC > Các ngôn ngữ lập trình khác (CCS C, HT 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

 
 
Ðiều Chỉnh Xếp Bài
Prev Previous Post   Next Post Next
Old 26-10-2014, 11:13 PM   #3
nhamtung
Nhập môn đệ tử
 
Tham gia ngày: Oct 2014
Bài gửi: 1
:
em mới học pic nên chưa biết nhiều mong các bác giúp đỡ em.em muốn chuyển đoạn code của mạch phân loại màu sắc sử dụng con tcs3200 từ atmega8 sang pic 16f877a.ai biết giúp em với
Chip type : ATmega8
Program type : Application
AVR Core Clock frequency: 8.000000 MHz
Memory model : Small
External RAM size : 0
Data Stack size : 256
************************************************** ***/
#include <mega8.h>
#include <delay.h>

#define FILTER_RED 0
#define FILTER_BLUE 1
#define NO_FILTER 2
#define FILTER_GREEN 3
#define RED 1
#define BLUE 2
#define GREEN 3

#define ON 0
#define OFF 1

#define DDROUT 1
#define DDRIN 0

#define TCS3200_S2 PORTC.0
#define DDR_TCS3200_S2 DDRC.0
#define TCS3200_S3 PORTC.1
#define DDR_TCS3200_S3 DDRC.1
#define TCS3200_S0 PORTC.2
#define DDR_TCS3200_S0 DDRC.2
#define TCS3200_S1 PORTC.3
#define DDR_TCS3200_S1 DDRC.3
#define TCS3200_OE PORTC.4
#define DDR_TCS3200_OE DDRC.4
#define TCS3200_OUT PORTD.5
#define DDR_TCS3200_OUT DDRD.5

#define LED_RED PORTB.3
#define LED_BLUE PORTB.2
#define LED_GREEN PORTB.1
#define DDR_LED_RED DDRB.3
#define DDR_LED_BLUE DDRB.2
#define DDR_LED_GREEN DDRB.1


unsigned int F_Blue=0,F_Green=0,F_Red=0,F_Clear=0;
unsigned long F;
unsigned int Counter_Timer0=0;
unsigned char Counter_Timer1=0;

//Tao 50ms va tinh toan tan so
interrupt[TIM0_OVF]void Time_Sample(void)
{
//Reset lai bien dem
TCNT0=255-250+5;
Counter_Timer0++;
if(Counter_Timer0==200)
{
//Dung timer0,Timer1
TCCR0=0b00000000;
TCCR1B=0b00000000;
//Sau 50ms tinh toan tan so
F=(TCNT1+(unsigned long)Counter_Timer1*65536)*20/1000;
Counter_Timer0=0;
Counter_Timer1=0;
//Reset lai bien dem Timer0,Timer1
TCNT0=255-250+5;
TCNT1=0;
//Bat timer0,Timer1
TCCR0=0b00000010;
TCCR1B=0b00000110;
}
}
interrupt[TIM1_OVF] void Counter_Pulse(void)
{
Counter_Timer1++;
}
void Timer1_Init(void);
void Timer0_Init(void);
unsigned int TCS3200_Read(unsigned char Temp);
void Check_Color(void );
void TCS3200_Setup(void);

void main(void)
{
TCS3200_Setup();
Timer1_Init();
Timer0_Init();
#asm ("sei")
while (1)
{
Check_Color();
}
}
void Timer1_Init(void)
{
//Dat gia tri ban dau
TCNT1=0;
//Timer1 che do thuong
TCCR1A=0b00000000;
//chon tan so,nguon vao la chan T1
TCCR1B=0b00000110;
//Cho phep ngat tran
//TIMSK|=0b00000100;
}
void Timer0_Init(void)
{
//Chon clock=CLK/8 T=1us
TCCR0=0b00000010;
TCNT0=255-250+5;
TIMSK=0b00000101;
}
void Check_Color(void )
{
F_Red=TCS3200_Read(FILTER_RED);
F_Green=TCS3200_Read(FILTER_GREEN);
F_Blue=TCS3200_Read(FILTER_BLUE);
if((F_Red>F_Green)&&(F_Red>F_Blue))
{
LED_RED=ON;
LED_GREEN=OFF;
LED_BLUE=OFF;
}
if((F_Green>F_Red)&&(F_Green>F_Blue))
{
LED_RED=OFF;
LED_GREEN=ON;
LED_BLUE=OFF;
}
if((F_Blue>F_Red)&&(F_Blue>F_Green))
{
LED_RED=OFF;
LED_GREEN=OFF;
LED_BLUE=ON;
}
}
unsigned int TCS3200_Read(unsigned char Temp)
{
if(Temp==FILTER_RED)
{
TCS3200_S2=0;
TCS3200_S3=0;
}
if(Temp==FILTER_BLUE)
{
TCS3200_S2=0;
TCS3200_S3=1;
}
if(Temp==NO_FILTER)
{
TCS3200_S2=1;
TCS3200_S3=0;
}
if(Temp==FILTER_GREEN)
{
TCS3200_S2=1;
TCS3200_S3=1;
}
delay_ms(300);
return F;
}
void TCS3200_Setup(void)
{
;
//F out la 100%
TCS3200_S0=1;
TCS3200_S1=1;
//Cho phep xuat tan so
TCS3200_OE=0;
//Hien thi
DDR_LED_RED=DDROUT;
DDR_LED_BLUE=DDROUT;
DDR_LED_GREEN=DDROUT;
}
nhamtung vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
 


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à 01:20 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