Nios® V/II Embedded Design Suite (EDS)
Support for Embedded Development Tools, Processors (SoCs and Nios® V/II processor), Embedded Development Suites (EDSs), Boot and Configuration, Operating Systems, C and C++
12604 Discussions

SPI ADC interface Nios 2, programing in C

Honored Contributor II

hey guys,

i am working on a project where i am supposed to pick up data from a "line sensor" QRE1113 from sparkfun and use MCP3002 ADC to convert the anolog signal to a binary value. i am using the DE1 board and i have built in the SPI (3 wire serial) core into my Qsys. i am trying to read and write data using:


IOWR(SPI_BASE, register, Value);


or example to write to the adc input [



this is my code as it stands 


#include "io.h"

#include "alt_types.h"

#include "system.h"

#include "altera_up_avalon_character_lcd.h"

#include "altera_up_avalon_character_lcd_regs.h"

#include "altera_avalon_spi_regs.h"

#include "altera_avalon_spi.h"

#include <stdio.h>

int main(){

while (1){

int rddata;

char buff [50];

int value ;

unsigned long i; //stay shutdown

for(i=0; i<1000000; i++){} //for counter interval

IOWR(SPI_BASE,5, 0x0001); //restart ADC

IOWR(SPI_BASE,1,0xf000); // initialize ADC,

unsigned long j;

for(j=0; j<100000; j++){} //buffer for spi RXDATA register to fill

rddata = IORD(SPI_BASE,0);//read data from RXdata

IOWR(SPI_BASE,5, 0x0000); //shutdown ADC

printf("%d", rddata);//print to nios consol

value=sprintf (buff, "%d", (rddata*100)/511 );

alt_up_character_lcd_dev * char_lcd_dev;

char_lcd_dev = alt_up_character_lcd_open_dev ("/dev/LCD");


alt_up_character_lcd_string(char_lcd_dev,"reflection= ");//print "reflection= "

alt_up_character_lcd_set_cursor_pos(char_lcd_dev,13,0);//move cursor

alt_up_character_lcd_string(char_lcd_dev, buff);//print data to LCD

alt_up_character_lcd_set_cursor_pos(char_lcd_dev,15,0);//move cursor

alt_up_character_lcd_string(char_lcd_dev,"%");//print "%"




i am using the DE1 board and asigned mosi, miso, sclk, and ss, to pins on the GPIO


i would apreciate all the help

0 Kudos
0 Replies