Protegemed / Mbed 2 deprecated mcp3201

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mcp3201.h Source File

mcp3201.h

00001 /*
00002     Header for MCP3201 Analog SAR Converter with SPI interface 
00003     Version: 1.0
00004     Julio S.
00005 */
00006 
00007 // SPI clock freq. in HZ
00008 // Limits from MCP3201 datasheet are:
00009 // 1500000Hz when vdd=5V
00010 // 800000Hz when vdd=2.7V
00011 // 1MHz should work and is the default value.
00012 #define MCP3201_CLK_FREQ    1000000
00013 
00014 // MCP3201 SPI channel pinout
00015 // p5 not wired, there are no commands to send for MCP3201 device
00016 SPI MCP3201(p5,p6,p7);
00017 // take control from CS pin to avoid transition changes between write commands
00018 DigitalOut cs(p8); 
00019 
00020 
00021 // Read the analog value from MCP3201.
00022 // return 12bit interger (0...4095)
00023 int ReadAnalogMCP3201(void)
00024 {
00025  //uncommente below line only if the MCP3201_CLK_FREQ is diferrent of 1MHz
00026  //MCP3201.frequency(MCP3201_CLK_FREQ);
00027  
00028  cs=0;
00029  int high_byte = MCP3201.write(0x00);
00030  int low_byte = MCP3201.write(0x00);
00031  cs=1;
00032  int conv_result = ((high_byte & 0x1f) << 7) | ((low_byte >> 1) & 0x7f);  
00033 
00034  return conv_result;
00035 }