Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
9 years, 9 months ago.
How to update data stream continuously when interruption occure.
Aim to display several data stream that is coming from other device on 7 Seg and altering it by tactile button using pin detect (interruption). However, when button is pressed it displays the data of pressed moment. want to show the data stream constantly. what is wrong? Following is the code. Appriciate for any help.
#include "PinDetect.h"
#include "mbed.h"
#include "SevenSegLed.h"
#define SLVADRS (0x20<<1)
// common type (0:anode common 1:cathode common)
// |
// | display mode (0:smooth 1:hard)
// | |
// | | segA segB segC segD segE segF segG segP com1 com2 com3 com4 (com5,com6,com7,com8 = NC)
// | | | | | | | | | | | | | |
SevenSegLed sevenSegLed(0, 1, dp11, dp9, dp4, dp2, dp1, dp10, dp6,dp24,dp25, dp18, dp17,dp24); // OSL40562-LR
I2CSlave slave(dp5, dp27); // 1114 SDA,SCL
Serial pc(dp16, dp15);
DigitalOut led_Red( dp28 );
DigitalOut led_Yellow( dp26 );
DigitalOut led_Blue( dp13 ); //13 //16
//================================
// display buffer
//================================
// com1
// | com2
// | | com3
// | | | com4
// | | | |
uint8_t D_7seg1[3] = {0x4, 0x3, 0x0}; // 0x0 to 0x9 = "0" to "9" ,0xA to 0xF = "A" to "F", 0x10 = extinction
uint8_t D_7seg2[3] = {0x4, 0x3, 0x1};
uint8_t D_7seg3[3] = {0x4, 0x3, 0x2};
uint8_t D_7seg0[3] = {0x0, 0x0, 0x0};
uint8_t D_dot[3] = {0, 0, 0}; // 0:extinction 1: light uint8_t D_dot[4] = {0, 1, 0,0};
PinDetect pin( dp14 );
int volatile count= 0;
//==============================
// main
//==============================
void myCallback( void ) {
count++;
if ( count == 3){
count =0;
}
if ( count == 0 ){
led_Red = 1;
led_Yellow = 0;
led_Blue = 0;
pc.printf("(count=%d",count);
pc.printf("\t");
pc.printf("Red\t");
pc.printf("value=%d",((char*)&D_7seg1)[0]); //&D_7seg[0]
pc.printf("%d",((char*)&D_7seg1)[1]);
pc.printf("%d)",((char*)&D_7seg1)[2]);
pc.printf("\t");
uint8_t V1[3] = {((char*)&D_7seg1)[0],((char*)&D_7seg1)[1],((char*)&D_7seg1)[2]};
sevenSegLed.SevenSegLed_main(V1,D_dot);
wait(1.0);
}
if ( count == 1 ){
led_Red = 0;
led_Yellow = 1;
led_Blue = 0;
pc.printf("(count=%d",count);
pc.printf("\t");
pc.printf("Yellow\t");
pc.printf("value=%d",((char*)&D_7seg2)[0]);
pc.printf("%d",((char*)&D_7seg2)[1]);
pc.printf("%d)",((char*)&D_7seg2)[2]);
pc.printf("\t");
uint8_t V2[3] = {((char*)&D_7seg2)[0],((char*)&D_7seg2)[1],((char*)&D_7seg2)[2]};
sevenSegLed.SevenSegLed_main(V2,D_dot);
wait(1.0);
}
if ( count == 2 ){
led_Red = 0;
led_Yellow = 0;
led_Blue = 1;
pc.printf("(count=%d",count);
pc.printf("\t");
pc.printf("Blue\t");
pc.printf("value=%d",((char*)&D_7seg3)[0]);
pc.printf("%d",((char*)&D_7seg3)[1]);
pc.printf("%d)\n\r",((char*)&D_7seg3)[2]);
uint8_t V3[3] = {((char*)&D_7seg3)[0],((char*)&D_7seg3)[1],((char*)&D_7seg3)[2]};
sevenSegLed.SevenSegLed_main(V3,D_dot);
wait(1.0);
}
}
int main() {
char buf[6];
uint16_t hoge1 = 0;
uint16_t hoge2 = 0;
uint16_t hoge3 = 0;
led_Red = 1 ;
led_Yellow = 0;
led_Blue = 0;
sevenSegLed.SevenSegLed_main(D_7seg1,D_dot);
pc.printf("(count=%d",count);
pc.printf("\t");
pc.printf("Red\t");
pc.printf("value=%d",D_7seg1[0]);
pc.printf("%d",D_7seg1[1]);
pc.printf("%d)",D_7seg1[2]);
pc.printf("\t");
//pc.printf("value=%d",((char*)&D_7seg1)[0]);
//pc.printf("%d",((char*)&D_7seg1)[1]);
//pc.printf("%d)\n\r",((char*)&D_7seg1)[2]);
pin.mode( PullDown );
pin.attach_asserted( &myCallback );
pin.setSampleFrequency();
slave.address(0x07<<1);
while (1) {
//wait(0.5);
int i = slave.receive();
switch (i) {
case I2CSlave::WriteAddressed:
int read_ret = slave.read(buf, 6);
//red
((char*)&hoge1)[0] = buf[0];
((char*)&hoge1)[1] = buf[1];
D_7seg1[0] = hoge1 % 1000 / 100;
D_7seg1[1] = hoge1 % 100 / 10;
D_7seg1[2] = hoge1 % 10;
// Yellow
((char*)&hoge2)[0] = buf[2];
((char*)&hoge2)[1] = buf[3];
D_7seg2[0] = hoge2 % 1000 / 100;
D_7seg2[1] = hoge2 % 100 / 10;
D_7seg2[2] = hoge2 % 10;
//Blue
((char*)&hoge3)[0] = buf[4];
((char*)&hoge3)[1] = buf[5];
D_7seg3[0] = hoge3 % 1000 / 100;
D_7seg3[1] = hoge3 % 100 / 10;
D_7seg3[2] = hoge3 % 10;
break;
}
}
}
1 Answer
9 years, 9 months ago.
Problem that will also probably occur is there is a Ticker interrupt in the sevensegled library code that will cause a disruption in your i2c data low if this triggers an interrupt during i2c communication. This can cause loss of i2c data or even for the system to lock up completely.
I would suggest a rethink here, generally keep the interrupt call back code as short as possible, set a value here and test that in your main program to run the display routine. Remove the Ticker function from the display driver and call that in your main program as well. I have used a similar led driver and had much the same problem with the Ticker clashing.
Please use
<<code>> and <</code>>to make your code readable. See the editing tips link for details and when possible check the preview before posting.From what I can make out you have a wait in the interrupt callback, that is almost always a very very bad thing to do.
posted by Andy A 19 Jan 2016