What PWM mode to select and how?

I am new to the embedded system programming environment. Can someone explain as to what PWM mode should I select in order to capture values at every rising edge/falling edge and at the mid bit? And how? I have been going through the ATMEGA328 datasheet but in vain. I am not sure what mode to select for the above.

Not clear to me what you’re trying to do, you talk about capture, but also PWM. Which is it? Details please.

PWM for setting bits in TCCR1A/TCCR1B. After generation of a square wave oscillating at 125kHz, incoming data gets superimposed on it. I want to read that data.
I have no clue what to put in these registers for initialization.

Dumb question: When and why do we use PWM settings?

TCCR1A = 0;
TCCR1B = (1<<ICNC1)|(1<<ICES1)|(CS12)|(CS11)|(CS10);//Setting noise canceller delays the ICES1/input capture by 4 oscillator cycles
TCNT1 = 0;
TIMSK = 0;
ICR1 =
OCR1A =

Hmm, that was more appropriate to your question than I thought.

Can you give more detail of what the specs and constraints are here? Do you have a timing diagram for the signal you're looking at and when you need to sample at? What do you mean superimposed on it? Are you generating some sort of signal, or reading one? Is there two lines, a data and a clock line?

I have attached a timing diagram below. The bit rate is 4.194 bits/sec. The bit duration is RF/32.

The 125 kHz square wave is the output of an analog front end - transceiver. The message signal is biphase encoded information that is superimposed upon interception.

So what is this signal? This is an input that you're trying to read or what? Or is it an output you're trying to duplicate?

That looks like an analog signal; you can't do analog reads fast enough for that, I think you're going to need some sort of external hardware here. What device is this that you're interfacing with?

I'm 95% sure you're in X-Y problem territory at this point.

Yes an input at the transceiver end. The design is a combination of signal processing and conditioning circuits. Design reads data from an - RFID tag.

What does the signal look like on the oscilloscope? What are the voltages involved? Like - could you smooth it and direct it to an opamp or comparator to convert that awful 125khz-modulated signal turned into something civilized?

Doing this in arduino seems really nasty.

Not sure what the waveform looks like as of now. In the start I was trying to configure an IC that was supposed to output a 125kHz square wave. Somehow the attempt went kaput.

The smoothening and comparator is all done. All I need to do try to read the data from the intercepted signal. Arduino is by far the easiest for me at this time.But it can be later implemented on better ATMEGAs once I get a grip of it.

https://forum.arduino.cc/index.php?topic=525073.0

Hi,
I don’t understand how the data are encoded from the picture you posted. Anyway please look in “How to use this forum” to learn how to inline pictures - when people don’t need to download them they are more likely to look at them and help you:

If I understand it correctly you supply the IC (EM4097) with 125kHz carrier frequency, it modulates it to the lower trace, than demodulates it back to the upper trace you are trying to read. So basicly you have two independent tasks:

  1. Generate 125kHz carrier
  2. Read the 4kHz output
    For generating the 125kHz carrier signal you can use nearly any timer mode with variable TOP. If you want to use other Arduino functions (such as millis, delay and many dependant functions) don’t use Timer0. Since this is simple task you may use Timer2 easily. For 50% duty cycle I would use CTC mode with “Toggle OCnx on Compare Match” mode. For generating 125kHz you need Compare Match at 250kHz (2 toggles per carrier cycle). You get one Compare Match every timer overflow, so elected timer needs to overflow every 16Mhz (Arduino default speed) / 250kHz = 64 cycles. For example using OC2B (PD3, Digital 3) you use
DDRD|=(1<<PD3);
TCCR2A=(1<<COM2B0)|(1<<WGM20);
TCCR2B=(1<<CS20);
OCR2A=63;
OCR2B=0;  //any value between 0 and OCR2A will do

You can use Timer1 in similar manner but maybe you will want to use its ICR feature to read output from the chip.

Hi, Thank you for the suggestion and explanation. I really appreciate it. I will try the same and post my progress.

The output of the EM4097 IC can be anywhere in between 100-150kHz. The device (IC) generates that. But since I couldnt get it configured correctly, the PLL is not getting locked and I can only see noise components on the oscilloscope.

I did not understand how you had decided on the resolution. Are there any formulae to calculate the same? I did use some from the internet and ended up getting some bizzare values.

Why do you suggest I use CTC mode? I read in the datasheet that if the new value to be written to the OCR1A is lower than the current value of TCNT1, the counter will miss the compare match. Will that effect the decoding in any way?

The encoding is BiPhase(BPSK/Differential BPSK) at a rate of RF/32.

I think I still don't understand what you need. Please try to make a picture of 1) Signal you want Arduino to output 2) Signal what should Arduino analyze and how (when it should be read)

This appears to be an extremely comprehensive blog on an signal encoding, with particular relevance to RFID. It is in Chinese, but a huge amount consists of English language articles and anyway there is always Google translate : http://www.cnblogs.com/shangdawei/p/4834182.html Maybe the OP can find a better picture in it to replace the rather cryptic picture in post #4

Here’s a representation:

Bit duration - RF/32

Square wave (carrier frequency) - 125kHz

Modulating signal frequency - Not sure but around 4kHz

I couldn't locate any tutorials on how to insert inline images.

No problem. Here it is:

906bf36ec8577c3367a16419e988208f70935a3d.png

Your hand drawn picture is certainly more of a conventional representation of a signal encoding than that in post #4.
So a 0 is represented by a phase transition within the bit frame and a 1 by no transition. Is that correct ?
The modulated signal frequency is 1/0.23845 kHz (4.19375 kHz) if the old diagram is correct.

Yes it is. I was able to generate a 125 kHz signal using a reference from the forum itself. What should I do for the clock and be able to read the data in (I m guessing this has to do with PWM)? I assume what I generated now is a 125kHz clock signal :fearful: ?

Below is the code:

//#include <avr/io.h>
//#include <util/delay.h>
//#include "delay_x.h"
#define F_CPU 16000000
#define CLK 12

const int ocr1aPin = 9;

// Set the frequency that we will get on pin OCR1A
void setFrequency(uint32_t freq)
{
  uint32_t requiredDivisor = (F_CPU/2)/(uint32_t)freq;
  uint16_t prescalerVal;
  uint8_t prescalerBits;
  if (requiredDivisor < 65536UL)
  {
    prescalerVal = 1;
    prescalerBits = 1;
  }
  else if (requiredDivisor < 8 * 65536UL)
  {
    prescalerVal = 8;
    prescalerBits = 2;
  }
  else if (requiredDivisor < 64 * 65536UL)
  {
    prescalerVal = 64;
    prescalerBits = 3;
  }
  else if (requiredDivisor < 256 * 65536UL)
  {
    prescalerVal = 256;
    prescalerBits = 4;
  }
  else
  {
    prescalerVal = 1024;
    prescalerBits = 5;
  }

  uint16_t top = ((requiredDivisor + (prescalerVal/2))/prescalerVal) - 1;
  TCCR1A = 0;
  TCCR1B = (1 << WGM12) | prescalerBits;
  TCCR1C = 0;
  OCR1A = (top & 0xFF);
}

// Turn the frequency on
void on()
{
  TCNT1H = 0;
  TCNT1L = 0;  
  TCCR1A |= (1 << COM1A0);
}

// Turn the frequency off and turn of the IR LED
void off()
{
  TCCR1A &= ~(1 << COM1A0);
}

void setup()
{
  digitalWrite(ocr1aPin, LOW);
  pinMode(ocr1aPin, OUTPUT);
  
}

void clk_setup(){
  
  pinMode(CLK, OUTPUT);
  digitalWrite(CLK, LOW);
}

void clk_reset(){
  clk_setup();
  delayMicroseconds(7812500000);
  digitalWrite(CLK, HIGH);
  delayMicroseconds(7812500000);
  digitalWrite(CLK, LOW);
  /*delayMicroseconds(7812500000);
  digitalWrite(CLK, HIGH);
  delayMicroseconds(7812500000);
  digitalWrite(CLK, LOW);*/
  
  }
  
void loop(){

  setFrequency(125000);
  on();
  delay(8);
  off();
  
  }

/*void floatDelay(float ms)
{
  unsigned long whole = ms;
  int part = (ms - t) * 1000;
  delay(whole);
  if (part > 4) delayMicroseconds(part);
}*/

My expertise is signal processing is limited. Where I had to process a specific AFSK signal, I found an algorithm written by an expert which was already adapted to 8 bit AVR architecture. In general, what I can say is this that you can configure a timer to trigger the ADC at intervals to sample the carrier. Since you are already at a frequency of 125 kHz that is quite high. You may require multiple samples within the carrier period. In this specific case, you are (somehow) going to have to detect a phase shift. From what I understand from your description of the signal, you'd be able synchronise your clock from the transition points in the 0 bits.

I will try again. I am new to AVR, but it's fun to learn although a struggle. Plus, when you are a beginner and ask stupid questions it is both embarassing and you are shamed. But anyhow, how do you synchronize the clock?

From what I know, I think I have to first generate a 500kHz clock signal with the 16MHz and use that very clock signal to generate the 125kHz carrier? :confused:

The clock synchronisation may be necessary if the frequency of the clock used to generate the signal may differ slightly from the clock you are using to analyse the signal. The window which you are using to view a bit might slip. Depending on the encoding scheme, this could be a problem especially if the same bit is repeated multiple times during which no transition occurs. Once a transition does occur, you can correct the slippage. Some encoding schemes force a transition every bit so then this is not a problem.

For analysing RFID related signalling, there is this tool if you haven't already seen it (RFIDler). It appears to do most of what you require. All the code is there together with a comprehensive description. The design is for the Pic32 architecture, but I guess you can reverse engineer it for an Arduino variant although maybe not an AVR 8 bit one.

RFIDler Powerpoint:

github: https://github.com/ApertureLabsLtd/RFIDler

source:

That is about the limit of what I can do here, but I'll be curious to see how you get on with it.