- #1
pranjal091994
- 5
- 0
Hi everyone can anyone help me in my code. I am trying to program a altera DE0 board to measure distance using a SRF05(ultrasonic range finder). I have written a code but now I am stuck.
Code:
#include "sys/alt_stdio.h" //for the alt_putstr function below. Outputs to Eclipse console
#include "altera_avalon_pio_regs.h" //for the I/O functions in the while loop below
#include "sys/alt_timestamp.h"
#include "system.h"
#define setHeaderOuts HEADEROUTPUTS_BASE+0x10 //HEADEROUTPUTS_BASE is defined in system.h of the _bsp file. It refers to the base address in the Qsys design
//the hex offset (in this case 0x10, which is 16 in decimal) gives the number of bytes of offset
//each register is 32 bits, or 4 bytes
//so to shift to register 4, which is the outset register, we need 4 * (4 bytes) = 16 bytes
#define clearHeaderOuts HEADEROUTPUTS_BASE+0x14 //to shift to register 5 (the 'outclear' register) we need to shift by 5 * (4 bytes) = 20 bytes, (=0x14 bytes)
// offset of 5 corresponds to the 'outclear' register of the PIO.
#define HEADERINPUTS_BASE+0x07
// this constant won't change. It's the pin number
// of the sensor's output:
const int pingPin = 5;
int main(void)
{
alt_putstr("This is the NIOS processor");
unsigned char buttons; //the buttons on the DE0
//unsigned char trigger;
unsigned int count;
const int clock = 50000000;
// establish variables for duration of the ping,
// and the distance result in inches and centimetres:
//long duration, inches, cm;
int seg, ts, delay;
while(1)
{
buttons=IORD_ALTERA_AVALON_PIO_DATA(PUSHBUTTONS1_2_BASE); //read the value of the pushbuttons
while((buttons & 0x01) == 1) // i.e. while pushbutton 1 is not pressed
{
buttons=IORD_ALTERA_AVALON_PIO_DATA(PUSHBUTTONS1_2_BASE); //read the value of the pushbuttons
}
alt_timestamp_start(); //clock starts
while((buttons & 0x01) == 1)
{
IOWR_ALTERA_AVALON_PIO_DATA(clearHeaderOuts,0x01); //turn off the first pin of the output port
IOWR_ALTERA_AVALON_PIO_DATA(setHeaderOuts,0x01); //turn on the first pin of the output port
delay = 12000000 //alt_timestamp_start(); //clock starts
count = alt_timestamp(); //records clock value
if (count > 10000000)
{
ts = IORD_ALTERA_AVALON_PIO_DATA(HEADERINPUTS_0x01); //reads from the first input pin of DE0
ts=count/clock //not sure about this function
IOWR_ALTERA_AVALON_PIO_DATA(SSEG_BASE,seg); //DE0 7 segment displays cm I think?
}
}
}
switch (ts)
{
case 0:
seg= 0xC0;
break;
case 1:
seg= 0xF9;
break;
case 2:
seg= 0xA4;
break;
case 3:
seg= 0xB0;
break;
case 4:
seg= 0x99;
break;
case 5:
seg= 0x92;
break;
case 6:
seg= 0x82;
break;
case 7:
seg= 0xF8;
break;
case 8:
seg= 0x80;
break;
case 9:
seg= 0x98;
break;
default:
seg= 0xC0;
break;
}
}