CS1180 's Arduino driver

Source: Internet
Author: User
Tags sdo

#define CS 2//cs1180 chip selection, low active

#define SDI3//cs1180 Data input

#define SDO4//cs1180 data output

#define SCLK5//cs1180 Clock

#define Drdy6//cs1180 data ready, low active


Write a Byte

void writespi_cs1180 (unsigned char DData)

{

unsigned char i;


Sclk=high;

Digitalwrite (Sclk,high);

for (i=8;i>0;i--)

{

sdi= (Bit) (DData & 0x80);

Digitalwrite (Sdi,highbyte (DData & 0x80));

ddata<<=1;

Sclk=low;

Digitalwrite (Sclk,low);

Delay5us ();

Delaymicroseconds (5);

Sclk=high;

Digitalwrite (Sclk,high);

Delay5us ();

Delaymicroseconds (5);

}

}


Read a Byte

unsigned char readspi_cs1180 (void)

{

unsigned char tempdata=0;

unsigned char i=0;


Sclk=high;

Digitalwrite (Sclk,high);

for (i=8;i>0;i--)

{

tempdata<<=1;

Sclk=low;

Digitalwrite (Sclk,low);

Delay5us ();

Delaymicroseconds (5);

TempData |=digitalread (SDO);

Sclk=high;

Digitalwrite (Sclk,high);

Delay5us ();

Delaymicroseconds (5);

}

return tempdata;

}


Initialize CS1180

void init_cs1180 ()

{

cs=0;

Digitalwrite (Cs,low);

writespi_cs1180 (0XFE); //send chip reset instruction

DELAY5MS ();

Delay (5);

writespi_cs1180 (0x50); //write register first address

writespi_cs1180 (0x02); //The number of write registers is 2+1=3

writespi_cs1180 (0x52);

writespi_cs1180 (0x06); //Gain 64

writespi_cs1180 (0x01); //normal use, configuration 01

writespi_cs1180 (0x24); //Bipolar

DELAY5MS ();

Delay (5);

while (Drdy);

while (Drdy);

while (Digitalread (Drdy) ==high);

while (Digitalread (Drdy) ==high);

Readspi_cs1180 (); //empty read once, do discard processing, because the first reading is not correct

DELAY5MS ();

Delay (5);

}


Read AD value

void getad_cs1180 ()

{

unsigned char ad[3];


while (Drdy);

while (Digitalread (Drdy) ==high);

writespi_cs1180 (0x01);

Delay5us ();

Delaymicroseconds (5);

Ad[0]=readspi_cs1180 ();

Ad[1]=readspi_cs1180 ();

Ad[2]=readspi_cs1180 ();

Ad[3]=pinswitch;

/*

Sendchar (0x0C);

Sendchar (0x55);

Sendchar (ad[0]); //high Byte

Sendchar (ad[1]); //middle Byte

Sendchar (ad[2]); //low Byte

Sendchar (ad[3]); //low Byte

Sendchar ((0x0c+0x55+ad[0]+ad[1]+ad[2]+ad[3]) & 0xff);

*/

Serial.println ((ad[0]<<16) + (ad[1]<<8) +ad[2]);

Serial.println (ad[1]);

Serial.println (ad[2]);

}


void Setup ()

{

Pinmode (Cs,output);

Pinmode (Drdy,input);

Pinmode (Sclk,output);

Pinmode (Sdi,output);

Pinmode (Sdo,input);


Init_cs1180 ();


Serial.begin (9600);

}


void Loop ()

{

Getad_cs1180 ();

}



This article is from the "Drag Mud crab" blog, please be sure to keep this source http://novelcrab.blog.51cto.com/10186204/1641493

CS1180 's Arduino driver

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.