|
Tài trợ cho PIC Vietnam |
Giao tiếp USB, CAN, I2C, SPI, USART... Những giao tiếp được tích hợp trên PIC |
|
Ðiều Chỉnh | Xếp Bài |
13-01-2014, 02:50 AM | #1 |
Nhập môn đệ tử
Tham gia ngày: Nov 2013
Bài gửi: 2
: |
help me
Giúp e cái code mikroC này với. E ko hiểu cái chương trình con getADC().
E ko hiểu ngay chỗ Code:
SPI1_Write(0x06); channel = channel << 6; tmp = SPI1_Read(channel) & 0x0F; tmp = tmp << 8; tmp |= SPI1_Read(0); Bài này là Giao tiếp SPI với con ADC mcp3204. Nguyên code của nó đây: Code:
sbit Chip_Select_Direction at TRISC0_bit; sbit Chip_Select at RC0_bit; // LCD module connections sbit LCD_RS at RB5_bit; sbit LCD_EN at RB7_bit; sbit LCD_D4 at RB0_bit; sbit LCD_D5 at RB2_bit; sbit LCD_D6 at RB4_bit; sbit LCD_D7 at RB6_bit; sbit LCD_RS_Direction at TRISB5_bit; sbit LCD_EN_Direction at TRISB7_bit; sbit LCD_D4_Direction at TRISB0_bit; sbit LCD_D5_Direction at TRISB2_bit; sbit LCD_D6_Direction at TRISB4_bit; sbit LCD_D7_Direction at TRISB6_bit; // End LCD module connections unsigned int measurement, lastValue; void Init() { ANSEL = 0; // Configure AN pins as digital I/O ANSELH = 0; C1ON_bit = 0; // Disable comparators C2ON_bit = 0; // Init LCD Lcd_Init(); Lcd_Cmd(_LCD_CLEAR); Lcd_Cmd(_LCD_CURSOR_OFF); // measurement = 0; // Initialize the measurement variable // Init SPI SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV64, // Initialize PIC as master _SPI_DATA_SAMPLE_END, // Data sample at end _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH); Chip_Select_Direction= 0; // ClearBit(TRISC,0) Chip_Select= 0; // SetBit(PORTC,0) } unsigned int getADC(unsigned short channel) { // Returns 0..4095 unsigned int tmp; Chip_Select = 0; // Select MCP3204 SPI1_Write(0x06); // SPI communication using 8-bit segments channel = channel << 6; // Bits 7 & 6 define ADC input tmp = SPI1_Read(channel) & 0x0F; tmp = tmp << 8; // Get ADC value tmp |= SPI1_Read(0); Chip_Select = 1; return tmp; } void ProcessValue(unsigned int pv, unsigned short channel) { // Writes measured values to LCD char i, lcdRow, lcdCol; if(channel < 2) lcdRow=1; else lcdRow=2; if(channel%2 > 0 ) lcdCol=13; else lcdCol=4; // Converting the measured value into 4 characters // and writing them to the LCD at the appropriate place i = pv /1000 + 48; Lcd_Chr(lcdRow,lcdCol, i); pv %= 1000; i = pv /100 + 48; Lcd_Chr(lcdRow,lcdCol+1, i); pv %= 100; i = pv /10 + 48; Lcd_Chr(lcdRow,lcdCol+2, i); pv %= 10; i = pv + 48; Lcd_Chr(lcdRow,lcdCol+3, i); } // main procedure void main() { Init(); // Compilation le Lcd_Out(1,1,"Build : " __TIME__); Lcd_Out(2,1,"le " __DATE__); Delay_ms(2000); // Initialize SPI and LCD Lcd_Out(1,1,"C0= C1="); Lcd_Out(2,1,"C2= C3="); while(1) { measurement = getADC(0); // Get ADC result from Channel 0 ProcessValue(measurement,0); // Writes measured value to LCD Delay_ms(100); // Wait 100ms measurement = getADC(1); // Get ADC result from Channel 1 ProcessValue(measurement,1); // Writes measured value to LCD Delay_ms(100); // Wait 100ms measurement = getADC(2); // Get ADC result from Channel 2 ProcessValue(measurement,2); // Writes measured value to LCD Delay_ms(100); // Wait 100ms measurement = getADC(3); // Get ADC result from Channel 3 ProcessValue(measurement,3); // Writes measured value to LCD Delay_ms(100); // Wait 100ms } } thay đổi nội dung bởi: caddish12, 19-01-2014 lúc 12:02 AM. |
|
|