PIC Vietnam

PIC Vietnam (http://www.picvietnam.com/forum/index.php)
-   Giao tiếp USB, CAN, I2C, SPI, USART... (http://www.picvietnam.com/forum/forumdisplay.php?f=45)
-   -   help me (http://www.picvietnam.com/forum/showthread.php?t=33299)

tieulongnhan9x 13-01-2014 02:50 AM

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
  }
}


caddish12 19-01-2014 02:40 AM

Câu trả lời nằm trong datasheet của con chip đó, bạn phải đọc kỹ mới nắm dc chứ.
Nhìn vào hình ở dưới, Microchip chỉ cho bạn cách communicate với MCU

http://i1142.photobucket.com/albums/...ps02f2f9c6.png

Giải thích cho bạn nhé (nhìn vào hình):
Code:

SPI1_Write(0x06);
8 bit đầu tiên được truyền sẽ bao gồm 1-bit data start (high), 1 bit (high or low) để chọn signal or differential mode, 1 bit của D2-bit này k quan tâm đối với MCP3204. Vậy tổng cộng ta sẽ có 8 bit đầu tiên truyền theo MSB trước sẽ là 00000110 hay 0x06 (thực ra thì 0x07 cũng đươc)
Code:

channel = channel << 6;
Nhìn vào timing bạn có thể thấy D1,D0 được truyền ở MSB(phía bít lớn nhất or bit 7) -> thế nên kênh bạn chọn là 0,1,2,3 sẽ ở LSB (phía bit nhỏ nhất) nên bạn phải shift nó lên để nó nằm ở MSB, vậy thì bit 1, bit 2 được shift qua trái 6 thì bit 2 sẽ lên thành bit 7, bit 1 sẽ lên thành bit 6. Ví dụ, bạn chọn channel 1 tức là 00000001, dịch <<6 sẽ là 01000000, như ở trên bạn chọn channel 3 đi, thì D1D0 sẽ là 00000011, dịch <<6 sẽ là 11000000 -> vậy là đúng theo timming ở trên rồi nhé

Code:

 
tmp = SPI1_Read(channel) & 0x0F;
tmp = tmp << 8;                     
tmp |= SPI1_Read(0);

Cũng theo cái hình timming ở trên, thì 4 bit đầu tiên của thời gian D1 D0 được gửi sẽ cũng là 4 bit MSB của mcp3204, vậy nên chỉ có 4 bit cuối của dữ liệu nhận là đúng, và đây là 4 bit lớn nhất của mcp3204 (kết quả có 12 bit, bit 0->bit11), vậy nên bạn phải shift nó qua trái để thành 4 bit cao của 12bit tức là shift trái 8 bit (4+8=12), sau đó bạn cộng kết quả với giá trị của lần đọc tiếp theo.
Thân.


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