unsigned char readEEPROM(unsigned char address){
EEADRL=address;
CFGS=0;
EEPGD=0;
RD=1;
return(EEDATL);
}
void sendDDSword(int);
void writeEEPROM(unsigned char,unsigned char);
unsigned char readEEPROM(unsigned char);
unsigned char test;
void main(void){
WPUA = 0x00;
writeEEPROM(0,0x23);
test=readEEPROM(0);
while(1){
Sorry, split the reply in two so as to demonstrate the differance.Pommie's example is really a 'procedure' as it doesn't return anything:
Sorry, split the reply in two so as to demonstrate the differance.
Mike.
No worries, take your time.
Looked at the datasheet for the DDS and your code again and see that it actually requires 40 bits to be sent to it. First bit sent is the LSB of the frequency and last bit sent is the MSB of the phase, is this correct?
Mike.
Thanks Nigel, the description by breaking things down as you have is, I think, what I need most at the moment.I don't think the previous two answers have really addressed your question?.
In Ian's example:.....
As I've placed this code at the end of the code I need to add a prototype to the top.
The top of the code now looks like,
To test the function I added a call to it just before the while(1),Code:#define cal_freq 0x00501BD0L #define DDSdata AN0 #define DDSclock AN2 void sendDDSword(int); void writeEEPROM(unsigned char,unsigned char); void main(void){
Code:WPUA = 0x00; writeEEPROM(0,0x23); while(1){ }
I can compile this and it will run.......
You can try this for yourself.
Mike.
Edit, I missed out the GIE stuff as I don't have an IRQ yet and didn't want to tempt fate.
Not dumb at all, and correct.So, next dumb question - I'm assuming that the while(1){ area is what will hold the the equivalent of 'main' from the asm program?
/*--- Encoder lookup table ---*/
const signed char table[] = {0,-1,+1,0,+1,0,0,-1,-1,0,0,+1,0,+1,-1,0};
/*--- Initialise Encoder ---*/
void init_encoder(void)
{
ANSELH &= 0xfc; /* RB2 and RB3 as digital I/O */
IOCB = 0x0c; /* Interrupt on change enabled for RB2, RB3 */
encoder_count = 0;
RBIF = 0;
RBIE = 1; /* Interrupt on change enable */
}
/*--- Encoder function ---*/
void encoder_click(void)
{
static unsigned char previous = 0;
unsigned char temp;
temp = 5;
while(temp--){ /* debounce */
;
}
temp = PORTB; /* Read port */
temp >>= 2; /* Shift input to bit positions 1, 0 */
temp &= 0x03; /* Mask out bits */
previous <<= 2; /* shift the previous data left two places */
previous |= temp; /* OR in the two new bits */
encoder_count += table[(previous & 0x0f)]; /* Index into table */
}
/*--- End of File ---*/
/*--------------------------------------------------------------------
Title : Header file for encoder.c
Filename : encoder.h
----------------------------------------------------------------------*/
#ifndef ENCODER_H
#define ENCODER_H
/*--- Global encoder count ---*/
extern volatile int encoder_count;
/*--- Function prototype ---*/
void init_encoder(void);
void encoder_click(void);
#endif
/*--- End of File ---*/
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/gpio.h>
#include "ad9850.h"
#define DDS_PORT GPIOA
#define DDS_RCC RCC_GPIOA
#define DDS_CLOCK GPIO2
#define DDS_RESET GPIO3
#define DDS_LOAD GPIO4 // Also called FQ_UD (Frequency Update)
#define DDS_DATA GPIO5
static const float DDS_REF = 125e6;
void dds_setup(void)
{
rcc_periph_clock_enable(DDS_RCC);
gpio_set_mode(DDS_PORT, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL,
DDS_CLOCK | DDS_RESET | DDS_LOAD | DDS_DATA);
}
void dds_reset(void)
{
// Set everything low first
gpio_clear(DDS_PORT, DDS_CLOCK | DDS_RESET | DDS_LOAD | DDS_DATA);
// Pulse reset
gpio_set(DDS_PORT, DDS_RESET);
gpio_clear(DDS_PORT, DDS_RESET);
// Pulse clock
gpio_set(DDS_PORT, DDS_CLOCK);
gpio_clear(DDS_PORT, DDS_CLOCK);
// Pulse load
gpio_set(DDS_PORT, DDS_LOAD);
gpio_clear(DDS_PORT, DDS_LOAD);
}
void dds_write(uint8_t byte)
{
uint8_t i;
uint8_t bit;
for(i = 0; i < 8; i++) {
bit = ((byte >> i) & 1);
if(bit == 1)
gpio_set(DDS_PORT, DDS_DATA);
else
gpio_clear(DDS_PORT, DDS_DATA);
gpio_set(DDS_PORT, DDS_CLOCK);
gpio_clear(DDS_PORT, DDS_CLOCK);
}
}
void dds_update_freq(float freq)
{
// Updates DDS output frequency. Supply frequency in Hz.
uint32_t tuning_word = (freq * 4294967295UL) / DDS_REF;
dds_write(tuning_word & 0xFF);
dds_write((tuning_word >> 8) & 0xFF);
dds_write((tuning_word >> 16) & 0xFF);
dds_write((tuning_word >> 24) & 0xFF);
dds_write(0);
gpio_set(DDS_PORT, DDS_LOAD);
gpio_clear(DDS_PORT, DDS_LOAD);
}
#ifndef AD9850_H
#define AD9850_H
#include <stdint.h>
void dds_setup(void);
void dds_reset(void);
void dds_write(uint8_t byte);
void dds_update_freq(float freq);
#endif // AD9850_H
static const float DDS_REF = 125e6;
// and separately:
uint32_t tuning_word = (freq * 4294967295UL) / DDS_REF;
#define DDS_CONST 4294967295.0
#define DDS_REF 125000000.0
static const float DDS_FACTOR = DDS_CONST / DDS_REF;
// That should be worked out by the compiler and stored as a single value.
// And change the calc slightly
uint32_t tuning_word = ((float)freq * DDS_FACTOR);
#define DDS_CONST 4294967295.0
#define DDS_REF 125000000.0
#define DDS_FACTOR = DDS_CONST / DDS_REF;
// That should be worked out by the compiler and stored as a single value.
// And change the calc slightly
uint32_t tuning_word = ((float)freq * DDS_FACTOR);
I can't use the hardware SPI as I am converting the code to work on existing hardware, so bit banging it has to be.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?