In the below example how do I change it to read 12bit values instead of 8 bit.
I tried changing ADC_8_BPS to ADC_16_BPS;
I changed all the unsigned chars to unsigned ints etc but am getting an exception message when trying to run.
Any guidance would be appreciated.
I don't need the PWM just want to test the ADC at full res with the joystick
I know it's early days on this dev board but sure wish there was some documentation about the ADC lib functions and how they work with the hardware.
#include <xscope.h>
#include "analog_tile_support.h"
#include "pwm_tutorial_example.h"
#include "debug_print.h"
#define PWM_PERIOD 200 // Set PWM period to 2us, 500KHz
#define ADC_PERIOD 100000 // 1ms ADC trigger - Sample at 1KHz
#define PRINT_PERIOD 10000000 // 100ms printing rate
#define pwm_duty_calc(x) ((x * (PWM_PERIOD-2)) >> 8) //duty calc macro, 255 input = full scale
//Port definitions
//Note that these assume use of XP-SKC-A16 + XA-SK-MIXED-SIGNAL hardware
on tile[0]: port trigger_port = PORT_ADC_TRIGGER; //XD70 Port P32A bit 19
on tile[0]: port pwm_dac_port = XS1_PORT_1G; //XD22 PWM2 on mixed signal slice
void xscope_user_init(void) {
xscope_register(2,
XSCOPE_CONTINUOUS, "Joystick ADC2", XSCOPE_UINT, "8b value",
XSCOPE_CONTINUOUS, "Header ADC4", XSCOPE_UINT, "8b value");
xscope_config_io(XSCOPE_IO_BASIC);
}
void adc_pwm_dac_example(chanend c_adc, chanend c_pwm_dac)
{
timer t_adc_timer, t_print_timer;
unsigned int adc_time, print_time;
unsigned data[2]; //Array for storing ADC results
unsigned char joystick, header, header_old; //ADC values
debug_printf("Analog loopback demo started.\n");
at_adc_config_t adc_config = {{ 0, 0, 0, 0, 0, 0, 0, 0 }, 0, 0, 0 }; //initialise all ADC to off
adc_config.input_enable[2] = 1; //Input 2 is horizontal axis of the joystick
adc_config.input_enable[4] = 1; //Input 4 is ADC4 analog input on header
adc_config.bits_per_sample = ADC_8_BPS;
adc_config.samples_per_packet = 2; //Allow both samples to be sent in one hit
adc_config.calibration_mode = 0; //Normal ADC operation - disable self calibration
at_adc_enable(analog_tile, c_adc, trigger_port, adc_config);
c_pwm_dac <: PWM_PERIOD; //Set PWM period
c_pwm_dac <: pwm_duty_calc(0); //Set initial duty cycle
t_print_timer :> print_time; //Set print timer for first loop tick
print_time += PRINT_PERIOD;
t_adc_timer :> adc_time; //Set ADC timer for first loop tick
adc_time += ADC_PERIOD;
at_adc_trigger_packet(trigger_port, adc_config); //Fire the ADC!
while (1)
{
//Main loop. CPU will wait until one of these events occurs, then service it
select
{
case t_print_timer when timerafter(print_time) :> void:
if (header != header_old){ //only do if value on header input has changed
debug_printf("ADC joystick : %u\t", joystick);
debug_printf("ADC header : %u\r", header);
header_old = header;
}
print_time += PRINT_PERIOD;//Setup time for next print event
break;
case t_adc_timer when timerafter(adc_time) :> void:
c_pwm_dac <: pwm_duty_calc((unsigned int)joystick); //send joystick value to to PWM
at_adc_trigger_packet(trigger_port, adc_config); //Trigger ADC
xscope_probe_data(0, joystick); //send data to xscope
xscope_probe_data(1, header); //send data to xscope
adc_time += ADC_PERIOD; //Setup time for next ADC rx event
break;
case at_adc_read_packet(c_adc, adc_config, data): //if data ready to be read from ADC
joystick = (unsigned char) data[0]; //First value in packet
header = (unsigned char) data[1]; //Second value in packet
break;
}
}
}
int main()
{
chan c_adc, c_pwm_dac;
par { //two logical cores and ADC service hardware on channel ends
on tile[0]: adc_pwm_dac_example(c_adc, c_pwm_dac);
on tile[0]: pwm_tutorial_example ( c_pwm_dac, pwm_dac_port, 1);
xs1_a_adc_service(c_adc);
}
return 0;
}
How to change the resolution of ADC for analogue sliceKIT ex
-
- Member
- Posts: 8
- Joined: Fri Jan 03, 2014 9:45 pm
-
- XCore Expert
- Posts: 589
- Joined: Wed Feb 29, 2012 10:03 am
The answer to the following question in teh Q&A section should answer your question:
http://www.xcore.com/questions/2086/how ... nd-devices
http://www.xcore.com/questions/2086/how ... nd-devices
-
- Member
- Posts: 8
- Joined: Fri Jan 03, 2014 9:45 pm
Ok thanks.
Below working code is what I ended up with.
Couple of questions though (I'm a real nube as you can tell)
In the example mentioned in previous post
on tile[0]: clock cl = XS1_CLKBLK_2;
I don't find I need this for it to work. Is this becasue I am using the XP-SKC-A16 instead of the U device?
Also what is the trigger port about. I don't quite understand what this is referring to.
Is it a physical pin or an internal connection?
And another, what is the xs1_a_adc_service(c); function. I can't find the decleration info when right clicking in the editor.
TIA
#include "analog_tile_support.h"
#include "debug_print.h"
#define ADC_PERIOD 100000 // 1ms ADC trigger - Sample at 1KHz
#define PRINT_PERIOD 10000000 // 100ms printing rate
//Port definitions
//Note that these assume use of XP-SKC-A16 + XA-SK-MIXED-SIGNAL hardware
on tile[0]: port trigger_port = PORT_ADC_TRIGGER; //XD70 Port P32A bit 19
//on tile[0]: clock cl = XS1_CLKBLK_2;
void adc_example(chanend c)
{
timer t_print_timer;
unsigned int print_time;
// A value that is not possible to get from the 12-bit ADC
unsigned short current_value = 0xfff;
unsigned short new_value = 0;
debug_printf("ADC 12bit demo started.\n");
at_adc_config_t adc_config =
{
{
0, 0, 0, 0, 0, 0, 0, 0
}
, 0, 0, 0
}; //initialise all ADC to off
adc_config.input_enable[2] = 1; //Input 2 is horizontal axis of the joystick
adc_config.bits_per_sample = ADC_16_BPS;
adc_config.samples_per_packet = 1;
adc_config.calibration_mode = 0;
at_adc_enable(analog_tile, c, trigger_port, adc_config);
t_print_timer:> print_time;
print_time += PRINT_PERIOD;
at_adc_trigger_packet(trigger_port, adc_config); //Fire the ADC!
while (1)
{
unsigned data[1];
select
{
case t_print_timer when timerafter(print_time):> void: if (new_value !=current_value)
{
debug_printf("ADC value: %d\n", new_value >> 4);
current_value = new_value;
} print_time += PRINT_PERIOD;
break;
case at_adc_read_packet(c, adc_config, data): new_value = data[0];
at_adc_trigger_packet(trigger_port, adc_config);
break;
}
}
}
int main()
{
chan c;
par
{
//one logical cores and ADC service hardware on channel ends
on tile[0]: adc_example(c);
xs1_a_adc_service(c);
} return 0;
}
Below working code is what I ended up with.
Couple of questions though (I'm a real nube as you can tell)
In the example mentioned in previous post
on tile[0]: clock cl = XS1_CLKBLK_2;
I don't find I need this for it to work. Is this becasue I am using the XP-SKC-A16 instead of the U device?
Also what is the trigger port about. I don't quite understand what this is referring to.
Is it a physical pin or an internal connection?
And another, what is the xs1_a_adc_service(c); function. I can't find the decleration info when right clicking in the editor.
TIA
#include "analog_tile_support.h"
#include "debug_print.h"
#define ADC_PERIOD 100000 // 1ms ADC trigger - Sample at 1KHz
#define PRINT_PERIOD 10000000 // 100ms printing rate
//Port definitions
//Note that these assume use of XP-SKC-A16 + XA-SK-MIXED-SIGNAL hardware
on tile[0]: port trigger_port = PORT_ADC_TRIGGER; //XD70 Port P32A bit 19
//on tile[0]: clock cl = XS1_CLKBLK_2;
void adc_example(chanend c)
{
timer t_print_timer;
unsigned int print_time;
// A value that is not possible to get from the 12-bit ADC
unsigned short current_value = 0xfff;
unsigned short new_value = 0;
debug_printf("ADC 12bit demo started.\n");
at_adc_config_t adc_config =
{
{
0, 0, 0, 0, 0, 0, 0, 0
}
, 0, 0, 0
}; //initialise all ADC to off
adc_config.input_enable[2] = 1; //Input 2 is horizontal axis of the joystick
adc_config.bits_per_sample = ADC_16_BPS;
adc_config.samples_per_packet = 1;
adc_config.calibration_mode = 0;
at_adc_enable(analog_tile, c, trigger_port, adc_config);
t_print_timer:> print_time;
print_time += PRINT_PERIOD;
at_adc_trigger_packet(trigger_port, adc_config); //Fire the ADC!
while (1)
{
unsigned data[1];
select
{
case t_print_timer when timerafter(print_time):> void: if (new_value !=current_value)
{
debug_printf("ADC value: %d\n", new_value >> 4);
current_value = new_value;
} print_time += PRINT_PERIOD;
break;
case at_adc_read_packet(c, adc_config, data): new_value = data[0];
at_adc_trigger_packet(trigger_port, adc_config);
break;
}
}
}
int main()
{
chan c;
par
{
//one logical cores and ADC service hardware on channel ends
on tile[0]: adc_example(c);
xs1_a_adc_service(c);
} return 0;
}