News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

Serieel inlezen uit 4021

Started by henktimmerman47@gmail.com, Sep 17, 2022, 08:32 AM

Previous topic - Next topic

henktimmerman47@gmail.com

Serieel inlezen van data uit 4021 in de PIC68A lukt mij niet. Wie kan mij helpen?
Het schema in de bijlage is een windrichting meter, die de 4 bits code opslaat in de 4021 (D0 t/m D3). De code wordt door 4 x CNY70 gelezen van een cilinder, die aan de buitenkant gecodeerd is met 4 bits Gray code. Als je de schakelaar sch activeert dan wordt de code (4 bits) met SHIN ingelezen in de PIC (poort B1) en vervolgens knippert de led aantal keren (afhankelijk van de waarde van WindRichting). In de praktijk knippert de led 1x of 17x (1x knipperen betekent dat WindRichting = 0 en 17x knipperen dat Windrichting = 16). Ik vermoed dat de seriele koppeling niet goed werkt. Wat gaat er niet goed?
PS: 1A t/m 3A vormt een and poort (dan is de code 4x zwart, wat het Noorden aangeeft). In dat geval brandt D1 (is handig bij het installeren in de praktijk)

diebobo

Heb je de tools om de serial output te monitoren ? Een scoop bijvoorbeeld of een logic analyzer ? Een USB - Serieel printje kan ook iets helpen.

Daarnaast, waarom lees je de IC1 tm 4 niet rechtstreeks in op de PIC ? Wat is de meerwaarde van de 4021 ?

Sorry for non-english, not sure if allowed and / or TS understand anything other then Dutch.

trastikata

Quote from: diebobo on Sep 17, 2022, 12:45 PMSorry for non-english, not sure if allowed and / or TS understand anything other then Dutch.

I don't know if it is, but for sure one will get more help if the problem is understood by everyone.

Yasin

I don't understand what you want to do with this circuit. But there may be fatal errors in the schematic drawing. MCLR pin is free. Maybe it's set to RA5 in the software, but I don't think so. Because it's not tied to anything. Another error is that the 4021 IC receives the clock pulse from the CLKOUT pin of the MCU. With each blow, the 4021 IC takes one step further. The MCU will process a command every 4 pulses. In this case, according to 4021, the MCU will be late. I think the clock source for the 4021 IC should be an idle pin of the MCU.

henktimmerman47@gmail.com

Dank voor de tips.
Diebobo: Met de scoop zie ik geen activiteit op de datalijn. Het kan zijn dat ik geen goed triggersignaal heb. In de toekomst wil ik deze schakeling voor de windrichting (4 bits) uitbreiden met windsnelheid (8 bits), neerslag (4 bits) en zonlicht (8 bits). Dan ligt serieel inlezen volgens mij voor de hand.
Yasin: In de spec van de 4021 IC zie ik dat bij VDD = 5V de clock frequency fCL = 3 MHz. Is de 20 MHz van de PIC te hoog? Hoe realiseer ik dat een inactieve pin van de PIC een goede klokbron wordt voor de 4021?

Dompie

@henktimmerman47@gmail.com Dit is een Engels forum waar iedereen Engels schrijft (ook de niet Engels sprekende mensen) dus waarom vertaal jij jouw Nederlandse teksten niet in Engelse teksten met behulp van Google Translate.

Hieronder staat de vertaalde tekst via Google Translate.

@henktimmerman47@gmail.com This is an English forum where everyone writes English (including non-English speaking people) so why not translate your Dutch texts into English texts using Google Translate.

Johan

henktimmerman47@gmail.com

Thanks for the tips.
Diebobo: With the scope, I don't see any activity on the data line. I may not have a good trigger signal. In the future I want to extend this wind direction circuit (4 bits) with wind speed (8 bits), precipitation (4 bits) and sunlight (8 bits). Then I think serial reading is the obvious choice.
Yasin: In the spec of the 4021 IC I see that at VDD = 5V the clock frequency fCL = 3 MHz. Is the 20 MHz of the PIC too high? How do I realize that an inactive pin of the PIC becomes a good clock source for the 4021?

Yasin

If I understand it correctly. You want to make a circuit that detects the wind direction. And it's enough to have it in 4 bit resolution. The MCU itself is sufficient for this process. No need for 4021 IC. I can't speak english either. I hope I understood correctly. I have prepared an example for you. Proteus simulation file available. If it works for you, you can start here. If you want to continue further development, I recommend choosing a more up-to-date MCU with more pins.

https://dosya.co/43zdrck7k1v1/Rüzgar_Yönü.zip.html




henktimmerman47@gmail.com

Background information Weather station
Hi Yasin, thank you for your comment. I'll give some more background information.
Ultimately I want to make a weather station with 2 PICs, which are interconnected via a network.
PIC1 collects weather data such as wind direction (4 bits), wind speed (8 bits), precipitation (4 bits), sunlight (8 bits) and temperature (8 bits). With PIC 2 I can request and present data from PIC1. For the wind direction I use 4 sensors, which read the position of the wind direction. For the wind speed I count the number of revolutions per unit of time, for the precipitation I count the number of containers that are filled with rainwater and I measure the sunlight with an LDR. All those signals are read in by means of 4021 ICs and periodically serially read into a PIC 16F628A.
Question 1: How can I read a serial signal from a 4021 IC into a PIC?
The next problem is that the signals have to be read in periodically. Wind speed, wind direction, sunlight and temperature once every 20 sec. and precipitation once a day. This could be done by starting a clock at 00:00, which changes every 20 seconds during the day. gives an instruction to retrieve the relevant signals.
Question 2: How do you realize that from 00:00 a clock (?) every 20 sec. gives an instruction to get signals?
For me this is a project to gain more experience with PICs and Proton.
Sincerely Henk

henktimmerman47@gmail.com

Background information Weather station
Hi Yasin, thank you for your comment. I'll give some more background information.
Ultimately I want to make a weather station with 2 PICs, which are interconnected via a network.
PIC1 collects weather data such as wind direction (4 bits), wind speed (8 bits), precipitation (4 bits), sunlight (8 bits) and temperature (8 bits). With PIC 2 I can request and present data from PIC1. For the wind direction I use 4 sensors, which read the position of the wind direction. For the wind speed I count the number of revolutions per unit of time, for the precipitation I count the number of containers that are filled with rainwater and I measure the sunlight with an LDR. All those signals are read in by means of 4021 ICs and periodically serially read into a PIC 16F628A.
Question 1: How can I read a serial signal from a 4021 IC into a PIC?
The next problem is that the signals have to be read in periodically. Wind speed, wind direction, sunlight and temperature once every 20 sec. and precipitation once a day. This could be done by starting a clock at 00:00, which changes every 20 seconds during the day. gives an instruction to retrieve the relevant signals.
Question 2: How do you realize that from 00:00 a clock (?) every 20 sec. gives an instruction to get signals?
For me this is a project to gain more experience with PICs and Proton.
Sincerely Henk

Stephen Moss

Not sure how good the Dutch will be its via Google Translate but...
Maybe I am misreading the 4021 data sheet incorrectly but it looks to me like your have to...
1) Set the Parallel/Serial Select to 1
2) Wait 1 positive clock edge to load the Parallel Data
3) Set the Parallel/Serial Select to 0 clock out the data for the required number of clock pulses
4) Repeat.

Part of you problem as you have already indicated is that you are running the clock from the 20MHz oscillator for the PIC and the maximum input clock is 3.5MHz, running it at a much higher clock would produce either No or highly unpredictable results. Also taking the input directly off the oscillator pins does not provide a good clock signal, if you were to look at in on an oscilloscope you would see that it is probably about 1v peak to peak sitting 1 volt off ground, so you really need to find a slower clock with an amplitude that runs the full 0-5V.
You could use a Clock Oscillator as the main clock source module and a divider IC to reduce the clock down to a frequency the device 4021 can cope with to just toggle an I/O pin to generate the clock, that way you have more control over the frequency as I don't think any of the Serial commands will do what you want, you have to write your own code for reading the data.

Having said that if you want to connect multiple 4021's to a 16F628A and connect them all to the PICs RX pin then you will need to use a multiplexer to select which 4021 your are reading the data from.

Ik weet niet zeker hoe goed het Nederlands zal zijn via Google Translate, maar...
Misschien lees ik het 4021-gegevensblad verkeerd, maar het lijkt me dat je moet ...
1) Zet ��de Parallel/Serial Select op 1
2) Wacht 1 positieve klokflank om de parallelle gegevens te laden
3) Stel de Parallel/Serial Select in op 0 klok uit de gegevens voor het vereiste aantal klokpulsen
4) Herhaal.

Een deel van je probleem, zoals je al hebt aangegeven, is dat je de klok van de 20 MHz-oscillator voor de PIC laat lopen en de maximale ingangsklok 3,5 MHz is, het draaien op een veel hogere klok zou geen of zeer onvoorspelbare resultaten opleveren. Ook als je de ingang direct van de oscillatorpinnen haalt, krijg je geen goed kloksignaal, als je naar een oscilloscoop zou kijken, zou je zien dat het waarschijnlijk ongeveer 1v piek tot piek is als je 1 volt van de grond zit, dus je moet echt naar zoek een langzamere klok met een amplitude die de volledige 0-5V loopt.
Je zou een klokoscillator kunnen gebruiken als de hoofdklokbronmodule en een scheidings-IC om de klok terug te brengen tot een frequentie die het apparaat 4021 aankan door gewoon een I / O-pin te schakelen om de klok te genereren, op die manier heb je meer controle over de frequentie aangezien ik denk dat geen van de seriële commando's zal doen wat je wilt, je moet je eigen code schrijven om de gegevens te lezen.

Dat gezegd hebbende, als u meerdere 4021's op een 16F628A wilt aansluiten en ze allemaal op de PIC's RX-pin wilt aansluiten, moet u een multiplexer gebruiken om te selecteren van welke 4021 u de gegevens leest.

Quote from: henktimmerman47@gmail.com on Sep 19, 2022, 12:22 PMQuestion 1: How can I read a serial signal from a 4021 IC into a PIC?
The next problem is that the signals have to be read in periodically. Wind speed, wind direction, sunlight and temperature once every 20 sec. and precipitation once a day. This could be done by starting a clock at 00:00, which changes every 20 seconds during the day. gives an instruction to retrieve the relevant signals.
If you see my comments below about using a Timer Interrupt, then within your interrupt handler (ISR) you increment a variable then when the variable = 4320 (assuming precisely every 20 seconds) your read the Precipitation then clear to variable back to 0.

Als je mijn opmerkingen hieronder ziet over het gebruik van een Timer Interrupt, dan verhoog je binnen je interrupt handler (ISR) een variabele en wanneer de variabele = 4320 (uitgaande van precies elke 20 seconden), lees je de neerslag en maak je de variabele terug naar 0.

Quote from: henktimmerman47@gmail.com on Sep 19, 2022, 12:22 PMQuestion 2: How do you realize that from 00:00 a clock (?) every 20 sec. gives an instruction to get signals?
For me this is a project to gain more experience with PICs and Proton.
That depends on how accurate the 20 seconds need to be, the simplest way is to put a delay in your main program loop (DelayMs 20000), you can always adjust that value to allow for the code execution time late so that it is exactly 20 Seconds.
A better way would be to use a Timer Interrupt, the clock frequency of the timer and the size of the timer (8 or 16 bit) will determine what value you need to write to it so that it overflows back to 0 and generates an interrupt. Your code then jumps to your interrupt handler (ISR) where you read the inputs and then returns back to your main program. 

Dat hangt af van hoe nauwkeurig de 20 seconden moeten zijn, de eenvoudigste manier is om een ��vertraging in uw hoofdprogrammalus te plaatsen (DelayMs 20000), u kunt die waarde altijd aanpassen om de uitvoeringstijd van de code mogelijk te maken, zodat het precies 20 is seconden.
Een betere manier zou zijn om een ��Timer Interrupt te gebruiken, de klokfrequentie van de timer en de grootte van de timer (8 of 16 bit) zullen bepalen welke waarde je ernaar moet schrijven zodat deze terug naar 0 overloopt en een interrupt genereert . Uw code springt dan naar uw interrupt handler (ISR) waar u de invoer leest en keert vervolgens terug naar uw hoofdprogramma.


See_Mos

#11
The clock pin should be under control of the PIC and connected to RA1 and not the crystal.

The PL pin is normally low and I think this clears the shift register as well.  Pulse PL low to high to low to load the wind direction.

RA1 is then used to clock the data into the PIC when PL is low.  Each time RA1 is high the data is read by checking the state of RB1.  Have a look at the SHIN command.

Unless you only have PIC16F628A then use a device with more pins and connect the wind direction sensors direct to the PIC.

See_Mos

I just noticed that this thread is in the wrong section and needs to be moved to Beginners or The Lounge

John Drew

Quote from: See_Mos on Sep 20, 2022, 02:29 PMI just noticed that this thread is in the wrong section and needs to be moved to Beginners or The Lounge
Moved to beginners
John

See_Mos

#14
This is the Proteus simulation for two 4021 connected to a PIC18F1220 or PIC18F1320

I have used resistor packs instead of individual pulldown resistors.  Unused inputs of the 4021 can be connected to ground. do not connect anything to Q5 and Q6 of the 4021.  the 4021 sends Bit 7 first so you will need to use Bits 4 to 7 or read all 8 bits.

Although this works it would be better to use MCP23016 or MCP23S17

See_Mos

Henk, Now I have looked at your code you just need to add the clock pin and change the code to

        High PS_Pin                ' load data into shift register first
        Low PS_Pin
        WindRichting = Shin DTA_Pin, CLK_Pin,MsbPre,4

and use input bits 4 to 7 of the 4021