Skip to content

Instantly share code, notes, and snippets.

@ti-nspire
Created July 6, 2018 23:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ti-nspire/b3d91a9268a2a3ef6c242d14ec8d5ff2 to your computer and use it in GitHub Desktop.
Save ti-nspire/b3d91a9268a2a3ef6c242d14ec8d5ff2 to your computer and use it in GitHub Desktop.
AD Converter MCP3008.c for PC-G850VS
unsigned int read(unsigned char CH){
unsigned char mosi[36] = {3,4,6,4,6,0,2,0,2,0,2,0,2,0,2,0,2,0,2,0,2,0,2,0,2,0,2,0,2,0,2,0,2,0,2,1};
unsigned char ch[8][6] = {{0,2,0,2,0,2}, /*CH0*/
{0,2,0,2,4,6}, /*CH1*/
{0,2,4,6,0,2}, /*CH2*/
{0,2,4,6,4,6}, /*CH3*/
{4,6,0,2,0,2}, /*CH4*/
{4,6,0,2,4,6}, /*CH5*/
{4,6,4,6,0,2}, /*CH6*/
{4,6,4,6,4,6}}; /*CH7*/
unsigned char miso[10];
unsigned char i, k;
unsigned int sum;
/* Some bits of the digital data which are to be transferred to ADC are replaced according to the input channel you specify.*/
for(i=0; i<6; i++){
mosi[i+5] = ch[CH][i];
}
/*The thirty-six of 3-bit digital data are transferred to ADC, and then the ADC will output each bit of a 10-bit digital code one at a time.*/
k = 9;
for(i=0; i<36; i++){
miniput(mosi[i]);
if((i>=16) && (i%2==0)){
miso[k] = miniget();
k--;
}
}
/*Some shifting operations can generate a 10-bit digital code.*/
sum = 0;
for(i=0; i<10; i++){
sum |= miso[i]<<i;
}
return sum;
}
main(){
unsigned int i; /*For PC-G850VS, busy wait does not need a "volatile" qualifier.*/
while(1){
printf("%4d, %4d, %4d, %4d\n", read(0), read(1), read(2), read(3));
for(i=0; i<500; i++){} /*busy wait*/
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment