hmmm… the timer or the shiftregister update are not working as i want… i want a stable blink…instead, it sometimes some blinks dont accour… it looks like the counter overlaps with the shiftregister update or so…maybe it is a priority question — any idea?
here is the code: (attached is also the full project - it runs on a lp17 core with dio-shiftregisters connected-dout register 1 pin 0 is the blink-output… the din register 1 pin 0+1 = hz-encoder… the used display is 2x40 signs…)
#include <mios32.h>
#include <FreeRTOS.h>
#include <task.h>
#include "tasks.h"
#include "file.h"
#include "app.h"
#include <portmacro.h>
#include <queue.h>
#include <semphr.h>
#include <string.h>
#include "mios32_timer.h"
#define NUM_ENCODERS 9 //5 Menue Encoders are connectet
const mios32_enc_config_t encoders[NUM_ENCODERS] = {//(SR begin with 1, ENC with 0) // setup the Pinout of that Encoders
{ .cfg.type=DETENTED2, .cfg.speed=FAST, .cfg.speed_par=4, .cfg.sr=1, .cfg.pos=0 }, //Menue Encoder 0 - only virtual, since they are mapped to differnt Variables...
{ .cfg.type=DETENTED2, .cfg.speed=FAST, .cfg.speed_par=4, .cfg.sr=1, .cfg.pos=2 }, //Menue Encoder 1 - ...if "Menue Page Encoder" has changed
{ .cfg.type=DETENTED2, .cfg.speed=FAST, .cfg.speed_par=4, .cfg.sr=1, .cfg.pos=4 }, //Menue Encoder 2
{ .cfg.type=DETENTED2, .cfg.speed=FAST, .cfg.speed_par=4, .cfg.sr=1, .cfg.pos=6 }, //Menue Encoder 3
{ .cfg.type=DETENTED2, .cfg.speed=FAST, .cfg.speed_par=4, .cfg.sr=2, .cfg.pos=0 }, //Menue Encoder 4
{ .cfg.type=DETENTED2, .cfg.speed=FAST, .cfg.speed_par=4, .cfg.sr=2, .cfg.pos=2 }, //Menue Encoder 5
{ .cfg.type=DETENTED2, .cfg.speed=FAST, .cfg.speed_par=4, .cfg.sr=2, .cfg.pos=4 }, //Menue Encoder 6
{ .cfg.type=DETENTED2, .cfg.speed=FAST, .cfg.speed_par=4, .cfg.sr=2, .cfg.pos=6 }, //Menue Encoder 7
{ .cfg.type=DETENTED2, .cfg.speed=FAST, .cfg.speed_par=4, .cfg.sr=3, .cfg.pos=0 },}; //Menue Page Encoder
xSemaphoreHandle xLCDSemaphore; // take and give access to LCD
u32 MenueUpdateCount = 0;
u32 value = 0; //Encoder calculation...
u32 i = 0; //Encoder calcualtion
//WKS
u32 hz = 428;
u32 nm = 10;
u32 rpm = 25680;
u32 time = 2161; //setup timer time
u32 timecount = 0;
u32 hzcount = 0;
u32 decaytime = 128;
//Task Prioritys
#define MUTEX_LCD_TAKE { while( xSemaphoreTakeRecursive(xLCDSemaphore, (portTickType)0) != pdTRUE ); } //a Mutex reserve a Resoure (LCD or SD-Card) for a task, until it is given away...
#define MUTEX_LCD_GIVE { xSemaphoreGiveRecursive(xLCDSemaphore); }
#define PRIORITY_StoreLoad ( tskIDLE_PRIORITY + 2 ) //3:Mios standart
static void StoreLoad(u8 tick, u16 order);
static void Timer(void);
void APP_Init(void){
MIOS32_BOARD_LED_Init(0xffffffff); //initialize all LEDs
xLCDSemaphore = xSemaphoreCreateRecursiveMutex(); //create Mutex for LCD access //initialize encoders i = counter
for(i=0; i<NUM_ENCODERS; ++i) MIOS32_ENC_ConfigSet(i, encoders[i]); //initialize encoders
MIOS32_TIMER_Init(1, 1, Timer, MIOS32_IRQ_PRIO_HIGH); //1uS Timer set
MIOS32_SRIO_ScanNumSet(1); // limit the number of DIN/DOUT SRs which will be scanned for faster scan rate
//speed up SPI transfer rate (was MIOS32_SPI_PRESCALER_128, initialized by MIOS32_SRIO_Init())
MIOS32_SPI_TransferModeInit(MIOS32_SRIO_SPI, MIOS32_SPI_MODE_CLK1_PHASE1, MIOS32_SPI_PRESCALER_64);
// prescaler 64 results into a transfer rate of 1 uS per bit
// when 2 SRs are transfered, we are able to scan the whole 16x8 matrix in 300 uS
}
void Timer(){
timecount = timecount + 1;
if (timecount > time) {MIOS32_DOUT_PinSet( 0, 1); timecount = 0;}
if (timecount > decaytime) {MIOS32_DOUT_PinSet( 0, 0);}}
void APP_Background(void){}
void APP_Tick(void) { //1ms Event Triggering
MenueUpdateCount = MenueUpdateCount + 1;
if(MenueUpdateCount > 650){MenueUpdateCount = 0; StoreLoad(0, 0);}} //update the Menue
void APP_MIDI_Tick(void){}
void APP_MIDI_NotifyPackage(mios32_midi_port_t port, mios32_midi_package_t midi_package){}
void APP_SRIO_ServicePrepare(void){}
void APP_SRIO_ServiceFinish(void){}
void APP_DIN_NotifyToggle(u32 pin, u32 pin_value){}
void APP_ENC_NotifyChange(u32 encoder, s32 incrementer){
if(encoder == 0){
value = hz + incrementer; // increment to virtual position and ensure that the value is in range 0..127
if(value < 0) {value = 999;}
if(value > 999){value = 0;}
hz = value;
time = 1000000/hz; //we count in mikro Sekonds!
rpm = hz * 60;
}
if(encoder == 2){
value = time + incrementer; // increment to virtual position and ensure that the value is in range 0..127
if(value < 0) {value = 3000;}
if(value > 3000){value = 0;}
time = value;
hz = 1000000/time;
rpm = hz * 60;
}
if(encoder == 4){
value = rpm + incrementer; // increment to virtual position and ensure that the value is in range 0..127
if(value < 0) {value = 100000;}
if(value > 100000){value = 0;}
rpm = value;
hz = rpm / 60;
time = 1000000/hz;
}
if(encoder == 7){
value = (incrementer * 2) + decaytime; // increment to virtual position and ensure that the value is in range 0..127
if(value < 0) {value = 99999;}
if(value > 99999){value = 0;}
decaytime = value;
}
}
void APP_AIN_NotifyChange(u32 pin, u32 pin_value){ //AIN - - Potentiometer has mooved - - Analog InPut
}
static void StoreLoad(u8 tick, u16 order){ //Display MENUE, Store+Load Bank & Sys-Config
if(tick == 0){//Menue
MUTEX_LCD_TAKE; //request LCD access
MIOS32_LCD_Clear(); //clear screen
//1st Line = Encoder Variables....change something!
MIOS32_LCD_CursorSet(0, 0); MIOS32_LCD_PrintFormattedString("%d", hz);
MIOS32_LCD_CursorSet(10, 0); MIOS32_LCD_PrintFormattedString("%d", time);
MIOS32_LCD_CursorSet(20, 0); MIOS32_LCD_PrintFormattedString("%d", rpm);
MIOS32_LCD_CursorSet(35, 0); MIOS32_LCD_PrintFormattedString("%d", decaytime);
//2nd Line = Menue Describtio
MIOS32_LCD_CursorSet(0, 1); MIOS32_LCD_PrintFormattedString("%s", "hz");
MIOS32_LCD_CursorSet(10, 1); MIOS32_LCD_PrintFormattedString("%s", "usec");
MIOS32_LCD_CursorSet(20, 1); MIOS32_LCD_PrintFormattedString("%s", "rpm");
MIOS32_LCD_CursorSet(35, 1); MIOS32_LCD_PrintFormattedString("%s", "decay");
MUTEX_LCD_GIVE;}} // release LCD access for other tasks
[RPM_BLINK.zip](< base_url >/applications/core/interface/file/attachment.php?id=12056)