PIC Vietnam

Go Back   PIC Vietnam > Truyền thông > Giao tiếp USB, CAN, I2C, SPI, USART...

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

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
Prev Previous Post   Next Post Next
Old 13-01-2014, 02:50 AM   #1
tieulongnhan9x
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);
tại sao lại viết như vậy?

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.
tieulongnhan9x 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à 07:09 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